IT Community - Software Programming, Web Development and Technical Support

If I return out of a try/finally in C#, does the code in the finally-clause run?

This is a discussion on If I return out of a try/finally in C#, does the code in the finally-clause run? within the C# Programming forums, part of the Software Development category; If I return out of a try/finally in C#, does the code in the finally-clause run?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-22-2007, 11:24 PM
H2o H2o is offline
D-Web Analyst
 
Join Date: Jul 2007
Posts: 246
H2o is on a distinguished road
Question If I return out of a try/finally in C#, does the code in the finally-clause run?

If I return out of a try/finally in C#, does the code in the finally-clause run?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-22-2007, 11:27 PM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Re: If I return out of a try/finally in C#, does the code in the finally-clause run?

Yes. The code in the finally always runs. If you return out of the try block, or even if you do a “go to” out of the try, the finally block always runs:
using System;
class main
{
public static void Main()
{
try
{
Console.WriteLine(\"In Try block\");
return;
}
finally
{
Console.WriteLine(\"In Finally block\");
}
}
}
Both “In Try block” and “In Finally block” will be displayed. Whether the return is in the try block or after the try-finally block, performance is not affected either way. The compiler treats it as if the return were outside the try block anyway. If it’s a return without an expression (as it is above), the IL emitted is identical whether the return is inside or outside of the try. If the return has an expression, there’s an extra store/load of the value of the expression (since it has to be computed within the try block).
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
gOS download...finally! kmcgra Technology BUZZzzzzz 0 11-17-2007 09:22 AM
Is the finally occurs when called the goto or return? S.Vinothkumar C# Programming 1 10-31-2007 03:11 AM
finally clause of a try-catch-finally statement vigneshgets Java Programming 2 08-16-2007 07:35 AM
What is the difference between a HAVING CLAUSE and a WHERE CLAUSE in SQL Server? mobilegeek Database Support 2 08-07-2007 08:04 AM
In the WHERE clause what is BETWEEN and IN? bluesky Database Support 2 08-07-2007 05:24 AM


All times are GMT -7. The time now is 10:23 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0