Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return in the Finally Block... Why not?

Tags:

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?

like image 988
serhio Avatar asked Apr 26 '11 09:04

serhio


People also ask

Can you return in a finally block?

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.

What happens if we have return statement in try block?

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.

Can we use return statement in try catch or 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.

Can we provide the statements after finally block if the control is returning from the finally block itself?

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.

Can we use return statement in finally block C#?

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.

Can we write return in try 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.


2 Answers

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

like image 63
Thomas Levesque Avatar answered Oct 15 '22 20:10

Thomas Levesque


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.

like image 36
Apeiron Avatar answered Oct 15 '22 21:10

Apeiron