I want to know that can we put a return statement in finally block in c#.
Thank you.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted May 23, 2012, 10:40 AM
So C# disallows it but, curiously, Java allows it even though it's bad practice for the reason stated above.
SenthilkumarPosted May 23, 2012, 2:52 AM
What is the use of putting return statement in finally block in C#.
The finally block is associated with the trycatch block and finally used to do clean up tasks.
The return statement can be out of the try catch block if method accepts to return the value.
It is not correct to do and not supported any way in C#.
Jignesh TrivediPosted May 22, 2012, 11:47 PM
C# Does not allow you to put return statement in side finally block.
Returning from inside a finally block will cause exceptions to be lost.
Please refer
http://weblogs.java.net/blog/staufferjames/archive/2007/06/_dont_return_in.html
hope this will help you..
Satyapriya NayakPosted May 22, 2012, 9:26 PM
No we cannot put return statement in finally block .
If finally block is being run as a effect of exception thrown from try block it doesn't make sense of returning any thing or leaving the finally block.
If any exception occurs in finally block it will replace original exception in try block.
Thanks