return statement inside finally block

  • Thread starter Thread starter midhun.prasad
  • Start date Start date
Sure you can. But it won't compile.

The implicit assumption of a finally block is to carry out cleanup code.
This guarantee can only hold true if there is no way to subvert the process.
A return, goto, jump, skip will subvert the process.

Consider a subversion example
try
{
//open database connection
}
finally
{
if some condition
return;

//close database connection isn't closed and leaks memory
}

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 
No, it is not legal. It is a branching statement. Put simply, you are
altering the flow of the code by bailing out during a clean up operation,
which is not permitted. As a finally ALWAYS runs, put the return outside of
the block.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Back
Top