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”);


    }


}


 

Comments (2)

  1. int19h says:

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

  2. 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