Sign in to follow this  

[.net] finally clause of C-sharp

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have got this lil confusion on the "finally" clause of C-sharp unfortunately i dont have the c-sharp compiler with me right now :S consider the following example
try{
...
...
}
catch(Exception e)
{
...
...
}
finally{
    Console.WriteLine( " i am inside the finally" );
}

Console.WriteLine( " i am outside the try block" );

If, there were no exception in the try block then shouldn't be the output like i am inside the finally i am ouside the try block The confusion is.. the book i am reading says the , "if there is no exception in the try block then the finally block will be executed right before the function goes out of scope" which my gut feelings says "NO".

Share this post


Link to post
Share on other sites
The code in finally will always execute. Exception throwed or not.

EDIT: The code in finally will always execute, and it will execute before any code below the finally block executes.

Share this post


Link to post
Share on other sites
well I know that finally will be always executed...but i want to know WHEN ? , i mean the order of execution is what i want to know... will it be executed before the function goes out ot scope or will it be executed right atfter the completion of the try block

Share this post


Link to post
Share on other sites
You're correct, the book is wrong (or maybe just improperly worded)

The finally clause will get executed when control flow leaves the try / catch block, regardless of whether there was an exception or not. So your output is correct.

What book is it that you're reading? We should submit an entry for its errata.



-Alias

Share this post


Link to post
Share on other sites
Quote:
Original post by browny
The confusion is.. the book i am reading says the , "if there is no exception in the try block then the finally block will be executed right before the function goes out of scope" which my gut feelings says "NO".


Your book is assuming that you would surround the entire function with the try-catch-finally block. The finally block will execute at the end of teh try or the end of the catch block. Any code after the finally block will be executed in sequential order...

In other words, the book is wrong.

Share this post


Link to post
Share on other sites
The "finally" block will be executed EVEN if there is an exception in the catch() block.

The whole point of "finally" is it always gets executed. Even if you return() from the function somewhere inside the try block (or in the catch block).

I frequently return from functions inside try blocks; it seems neat.

Mark

Share this post


Link to post
Share on other sites

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this