As MSDN mentions:
The code in a Finally block runs after a Return statement in a Try or Catch block is encountered, but before that Return statement executes. In this situation, a Return statement in the Finally block executes before the initial Return statement. This gives a different return value. To prevent this potentially confusing situation, avoid using Return statements in Finally blocks.
As I didn't understand a lot from this note, I'll take an example (VB.NET, I think in C# is the situation is similar):
Try HugeOp() Return "OK" Catch Return "NOK" Finally Return "Finally" End Try
Now, why should be this illegal in both C# and VB.NET?
Returning from inside a finally block will cause exceptions to be lost. A return statement inside a finally block will cause any exception that might be thrown in the try block to be discarded.
1. Return statement inside the try or catch block. If we have a finally block, the return statement inside try and catch block are not executed. It will always hit the finally block.
In a try-catch-finally block that has return statements, only the value from the finally block will be returned. When returning reference types, be aware of any updates being done on them in the finally block that could end up in unwanted results.
After the finally block is executed the statements following it get control. If the try block exits because of an Exception which is NOT handled by a catch block control goes directly to the finally block. After the finally block is executed the Exception is thrown to the caller and control returns to the caller.
In C#, multiple finally blocks in the same program are not allowed. The finally block does not contain any return, continue, break statements because it does not allow controls to leave the finally block.
you can use a return statement inside the try block, but you have to place another return outside the try block as well. If you pass true while calling sayHello method, it would return from try block. A return statement has to be at the method level instead of at any other specific level.
It's illegal because when you reach the Finally
block, the value to return is already defined ("OK" if everything went well, "NOK" if an exception was caught). If you were able to return a different value from the Finally
block, this value would always be returned, whatever the outcome of the instructions above. It just wouldn't make sense...
I was curious about this, I'm running VS2010 and does not allow a Return in the finally block. here is the code I compiled
Public Class Class1 Public Shared Function test() As String Try Return "OK" Catch ex As Exception Return "Catch" Finally test = "Finally" End Try End Function End Class
I compiled the DLL to view the MSIL it looked rather interesting the above code basically gets refactored to this:
Public Class Class2 Public Shared Function test() As String Try Try test = "OK" Catch ex As Exception test = "Catch" End Try Finally test = "Finally" End Try Return test End Function End Class
and testing this out, the MSIL for the above two classes is exactly the same.
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