Welcome to MSDN Blogs Sign in | Join | Help

Exception Handling without catch(Exception)

The design guidelines for exception handling are quite clear on avoiding “catch all” and/or avoiding catching exceptions you can’t handle. But there are cases when you really need to know if the try block completed successfully or not, and possibly take some action.

 

One way one could achieve this without breaking the guideline is to set a flag at the end of the try block and then query it inside finally:

 

bool workCompleted = false;

try

{

 

    DoSomething();

    DoSomethingMore();

    workCompleted = true;

}

finally

{

    if (!workCompleted)

    {

        Console.WriteLine("record that the work failed");

    }

}

 

Published Tuesday, May 06, 2008 11:58 PM by florinlazar
Filed under:

Comments

# re: Exception Handling without catch(Exception)

Wednesday, May 07, 2008 9:21 AM by int19h

Maybe we'll see the fault-blocks from IL in C# and/or VB.Net some day...

# re: Exception Handling without catch(Exception)

Wednesday, May 07, 2008 1:30 PM by dominick

Try/Finally (without catch) blocks may have problems because of the (esoteric and IMO just wrong) exception filtering behavior of the CLR.

http://blogs.msdn.com/shawnfa/archive/2006/03/03/542430.aspx

fyi.

dominick

Anonymous comments are disabled
 
Page view tracker