Return and finally offer - inconsistent behavior

I came across a slightly strange (IMO) code that behaves inconsistently.

try
{
   if (helperMethod())
   {
       return 0;
   }

   return 0;
 }
 catch(Exception e)
 {
    // Log and throw
 }
 finally
 {
     // Do a lot of stuff after value has been returned
 }

This sits inside a method that is called by VBA by passing a COM object to my DLL. When it only starts, I don't get an exception in C #, but I get a VBA exception.

When I run this in debugging, I get no exception anywhere.

My guess is that the logic in the finally clause takes second place to run, and at that time 0 is already returned by the main theme of the method.

I can rewrite this in several ways, but I don’t know if writing code this way is enough ...?

thank

EDIT: Is it possible that a COM object will be released when I return 0? In this case, it is no longer available in the finally clause.

+3
2

, ( ) finally . Framework :

finally , try.

try ( try-catch-finally, , ), finally , , .

+1

COMException CLR.NET, finally. EventLog.

WMI. , ...

+2

Source: https://habr.com/ru/post/1779404/


All Articles