difference between try...catch and try....finally ? in asp.net(C#)

like when i want to catch error like 1/0 then i put code in try block and put exception object in catch block like response.write("ERROR:"+ ex.Message) but advisors told me that it isn't a good practice to put catch always, it absorbs error without notifying ????? ehhhhh ? but it did via ex.Message , so why ? and what does try....finally do ? i know that it is used to release resources but of what use is TRY if exception can't be catched ?

有帮助吗?

解决方案 2

Eveything that is enclosed in your finally block is ensured to be executed, and it could be useful in these 2 concrete cases at least :

  • Sometimes you decide to call return in the middle of your try block and get back to the caller : finally ease the process of releasing ressources here, you don't have to write some specific code to go directly to the end of your method.
  • Sometimes you want to let an exception go up (by not catching it) and maybe being caught at a higher level (because it is not the appropriate place to handle it properly for example). Again finally ensures your resources are released and the exception continue its path.

Maybe you can see finally as a tool helping developpers to do things they're obliged to do with less effort. On the other side, catch is dedicated to handle errors.

Both keywords are dedicated to flow control, but they don't have the same purpose and they can be used one without each other (and often are!). It depends on your needs.

其他提示

try/catch/finally:

try
{
    // open some file or connection
    // do some work that could cause exception
}
catch(MyException ex)
{
   // do some exception handling: rethrow with a message, log the error, etc...
   // it is not a good practice to just catch and do nothing (swallow the exception)
}
finally
{
    // do some cleanup to close file/connection
    // guaranteed to run even if an exception happened in try block
    // if there was no finally, and exception happened before cleanup in your try block, file could stay open.
}

Try/Finally:

try
{
    // open some file/connection
    // do some work, where you're not expecting an exception
    // or, you don't want to handle the exception here, rather just let it go to the caller, so no need for a catch
}
finally
{
    // do cleanup, guaranteed to go in this finally block
}

Finally is always executed whether there is an exception or not. This can be handy if you want to be absolutely certain that something is cleaned up. Example:

void ReadFile(int index)
{
    // To run this code, substitute a valid path from your local machine 
    string path = @"c:\users\public\test.txt";
    System.IO.StreamReader file = new System.IO.StreamReader(path);
    char[] buffer = new char[10];
    try
    {
        file.ReadBlock(buffer, index, buffer.Length);
    }
    catch (System.IO.IOException e)
    {
        Console.WriteLine("Error reading from {0}. Message = {1}", path, e.Message);
    }

    finally
    {
        if (file != null)
        {
            file.Close();
        }
    }
    // Do something with buffer...
}

If you didn't have a finally in there it would be possible that the file would not be closed properly if an error occurred. Regardless of whether an error occurs or not, you want the file to be closed when you are done.

Consider the alternative:

void ReadFile(int index)
{
    // To run this code, substitute a valid path from your local machine 
    string path = @"c:\users\public\test.txt";
    System.IO.StreamReader file = new System.IO.StreamReader(path);
    char[] buffer = new char[10];
    try
    {
        file.ReadBlock(buffer, index, buffer.Length);
        file.Close();
    }
    catch (System.IO.IOException e)
    {
        Console.WriteLine("Error reading from {0}. Message = {1}", path, e.Message);
    }
}

If you error out on ReadBlock the file will not be properly closed.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top