I came across slightly odd (IMO) code whcih behaves inconsistantly.
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 which gets called by a VBA through by passing COM object to my DLL. When it just runs, I don't get exception in C#, but I get a VBA exception.
When I run this in a debug than I don't get exception anywhere.
My guess is that logic in a finally clause takes over a second to run, and at that time 0 has already been returned by main body of the method.
I can re-write this in few ways, but I don't knwo whether it's common to write code in such a way...?
Thank you
EDIT: Could it be that the COM object gets released when I return 0? In that case it's no longer available in a finally clause.
COMException can crash your .NET CLR engine so that engine does not get a chance to run the finally code. In these cases you will see an entry in the EventLog.
I have seen a lot of these with WMI. Evil, evil ...
I think here you may be trying (no pun intended) to use the finally block in an odd way. The Framework documentation typifies finally as follows:
The finally block is useful for cleaning up any resources allocated in the try block.
You should do your work within your try block (or outside the try-catch-finally construct if it's just covering a potential exception condition and not the subsequent processing of returned data) and your finally block should only release any resources allocated in the try that need to be released regardless of an exception or normal termination.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With