locked
finally block not executed RRS feed

  • Question

  •  

    Hello,

     

    Recently I've encountered a strage behavior on C#'s try...catch...finally. Sometimes the finally block does not get executed if there's no exception thrown in try block.

     

    pseudo code as follows:

     

    Code Snippet

    try

    {

    use Interop to new a COM class to do some hardware-related action

    }

    catch (COMException)

    {

    handling exception

    }

    finally

    {

    collecting data, closing hardware

    }

     

     

     

    If there's no exception in try block, the code in finally block is not always get executed. It seems that the CLR is completely ignore code in finally block. How would this happens?
    Friday, May 9, 2008 4:47 AM

Answers

  • The only reason a finally block won't get executed is if the thread is terminated abnormally (other than a corrupted stack--but the MDAs should catch that).

     

    You're saying that "handling exception" never gets called and "collecting data, closing hardware" sometimes doesn't get called?  Try to add some code after calling the COM class to ensure it's returning.

     

    Friday, May 9, 2008 1:17 PM
    Moderator
  • Hi dhchen,

    I guess some exception were not captured and handled, thus your program was terminated. And perhaps you can try the following code snippet to fix the issue.

    Code Snippet

                try

                {

                    

                  //use Interop to new a COM class to do some hardware-related action

                }

                catch (Exception)

                {

                    //handling exception

                }

                finally

                {

                    // do the finally process

                }

     

     

    For more information about COM Interop Exceptions, please check the LINKs below.

    http://msdn.microsoft.com/en-us/library/awy7adbx.aspx

    http://msdn.microsoft.com/en-us/library/9ztbc5s1.aspx

    Regards,

    Xun

     

    Tuesday, May 13, 2008 9:31 AM

All replies

  • it cann't happen. The catch (COMException) is not sufficient to catch the exception thrown in try block (I mean, some other exceptions are also being generated in the try block). To detect this add the following code after catch (COMException)

     

    Code Snippet

    Catch(Exception e)

    {

    Console.Writeline("Some Other Exception "+ e.PrintStakeTrace);

    }

     

     

     

    Note : Please ensure that all your Exception classes are some how sub-class of the Main Exception Class.

    Friday, May 9, 2008 6:35 AM
  • The finally block should always execute, regardless of wether you catch the exception or not.

    However, since you're calling a COM object, could it be that you've messed up the calling convention so that the stack is hosed? I'd imagine that could make .NET confused, but then I'd venture you'd have a lot more problems than just a missing finally block.

    Have you added code in all the parts of this to log that things are executed to make sure they are?
    Friday, May 9, 2008 7:32 AM
  • Are you sure that you aren't yourself causing (and swallowing) an exception in the finally? It should get executed, regardless of any (or none) "catch" blocks.

     

    Friday, May 9, 2008 7:41 AM
  • The only reason a finally block won't get executed is if the thread is terminated abnormally (other than a corrupted stack--but the MDAs should catch that).

     

    You're saying that "handling exception" never gets called and "collecting data, closing hardware" sometimes doesn't get called?  Try to add some code after calling the COM class to ensure it's returning.

     

    Friday, May 9, 2008 1:17 PM
    Moderator
  • Hi dhchen,

    I guess some exception were not captured and handled, thus your program was terminated. And perhaps you can try the following code snippet to fix the issue.

    Code Snippet

                try

                {

                    

                  //use Interop to new a COM class to do some hardware-related action

                }

                catch (Exception)

                {

                    //handling exception

                }

                finally

                {

                    // do the finally process

                }

     

     

    For more information about COM Interop Exceptions, please check the LINKs below.

    http://msdn.microsoft.com/en-us/library/awy7adbx.aspx

    http://msdn.microsoft.com/en-us/library/9ztbc5s1.aspx

    Regards,

    Xun

     

    Tuesday, May 13, 2008 9:31 AM