Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

If i put a return statement in a finally clause of a try catch finally block, i get an error like below:

Quote:
Cannot leave the body of a finally clause



Suppose:

C#
try
  {


   }

 catch(SomeException e)
  {



   }

 finally
  {


  return some_value;

  }


Can some one please explain the reason behind it? I removed the error. Bu i am inquisitive as to why does this happen? whats the reason behind it?

Thanks
Posted
Updated 25-Jan-14 2:54am
v2

Greetings,

The compiler does not allow you exit the function from a finally clause. You can have cleanup code in finally cluase.


C#
try
  {
 

   }
 
 catch(SomeException e)
  {
 
 
   }
 
 finally
  {
 //clean up code
 
  }

  return some_value;


Regards

Dominic
 
Share this answer
 
Comments
Rahul VB 25-Jan-14 7:18am    
Thanks a ton, i can do a small experiment: i will write a try-catch-finally block and see ILDASM code, what do you say?
The exception is going to exit the function, not the return. You can exit a function via a return or an exception, but not both. That''s why you can''t return from a finally.

you can do something like below:-

C#
 try
  {

   }

 catch(SomeException e)
  {

   }

 finally
  {
  
  }
return some_value;
 
Share this answer
 
Comments
Rahul VB 25-Jan-14 7:18am    
Thanks a ton
Not being able to use 'return in a 'finally block in C# is a language-design choice; other languages, like Java, JavaScript, allow it. In fact, both those languages allow returns in both catch and finally blocks: the last return defined is what's executed.

There are some exceptions, like OutOfMemoryException, that will terminate execution before a finally block code is executed, but, in general, the code in a finally block is guaranteed to execute. You should use a finally block to "clean up," to dispose of things that need disposing.

You can use 'return in the catch block, but, if you have a finally block and a catch block, that sets up the interesting possibility of a variable (declared outside the method) being modified in the catch block after a method has returned. You can see an example of that here: [^].

If you really want to go deep into how the .NET compiler under-the-hood transforms to try-catch-finally to IL code, the thread of the above-cited link is quite valuable.

Note that there are some people who have very strong feelings about the way error handling should be done in .NET, and that they advocate always throwing an exception up to some higher level exception handler. There are resources on advanced application-wide error handling strategies here on CodeProject if you care to look.

On a practical basis, I'd say that using return only after the finally block, if you have handled the error in a catch block, is a best practice.
 
Share this answer
 
Comments
Rahul VB 25-Jan-14 7:22am    
I should refer ILDASM code. I will do that shortly and get back. Thanks, very informative.
Ron Beyer 25-Jan-14 9:01am    
5, nice breakdown of it.
To avoid a potentially confusing situation, do not use a return statement in a finally block. The code in a finally block is run after a return statement in a try or catch block is encountered, but before that return statement is executed. In this situation, a return statement in the finally block is executed before the initial return statement, resulting in a different return value.

From http://msdn.microsoft.com/en-us/library/vstudio/k4hea629%28v=vs.100%29.aspx[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900