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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted May 22, 2012, 5:26 PM EDT
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
Jignesh TrivediRank #16 · 50.3K reputationPosted May 22, 2012, 7:47 PM EDT
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..
SenthilkumarRank #387 · 6.5K reputationPosted May 22, 2012, 10:52 PM EDT
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#.
VulpesRank #55 · 29.2K reputationPosted May 23, 2012, 6:40 AM EDT
So C# disallows it but, curiously, Java allows it even though it's bad practice for the reason stated above.