Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi all,
FInally block is not executing.I have something like this.

C#
int i = 123;
string s = "Some string";
object o = s;

try
{
    // Invalid conversion; o contains a string not an int
    i = (int)o;
}
finally
{
    Console.Write("i = {0}", i);
}




Problem is execution is not going to finally after exception.Thanks in advance.
Posted
Updated 29-Oct-13 1:27am
v2
Comments
ZurdoDev 29-Oct-13 7:28am    
Finally is guaranteed to work. Put a breakpoint and debug it.
ZurdoDev 29-Oct-13 7:36am    
What does that mean?
ZurdoDev 29-Oct-13 7:28am    
By the way, I try without a catch is silly in my opinion.

You have discovered (surprise !) an example of total stupidity on the part of MSDN: their example of try/finally is very mis-leading.

You get an error when you run your program in either Debug or Release mode from Visual Studio because of the way that Visual Studio works: so, yes: the code in the 'finally block is not executed.

If you go to the your project folder, and execute the 'exe file for your project: you will see the code in the 'finally block is executed ... if you do something like this in the 'finally block:

MessageBox.Show("running from the .exe I can see code in the 'finally is executed";

Your program will then crash right after displaying the message.

A 'try without a 'catch is a mistake !

Insert an empty 'catch in your code above, and you'll see that it keeps on trucking, and does execute the code in the 'finally block in Visual Studio.

However, writing an empty 'catch block is about as bad a mistake as not writing a 'catch block.

I wish that Visual Studio would never allow a 'try without both 'catch, and 'finally, and also never allow an "empty" 'catch block.

I hope you are not too shocked, or depressed, to find out MSDN is not perfect :)
 
Share this answer
 
Comments
Uday P.Singh 29-Oct-13 9:54am    
5!
DileepReddyC 29-Oct-13 9:59am    
Yes Mr BillWoodruff it is indeed misleading..Thank you...atleat some one understood my confusion
:-)
1. Understand the concept
2. Learn how to debug
3. Specify the source if you are using code samples.

Have a look into the below link, where you can find the exact code and explanation

http://msdn.microsoft.com/en-us/library/zwc8s4fz(v=vs.90).aspx[^]
 
Share this answer
 
Comments
Thanks7872 29-Oct-13 7:46am    
This is what he referred to and copied the codeblock.
Thanks7872 29-Oct-13 7:50am    
Sorry if you felt it personally. I didn't mean that. Removed it.
Finally block is executed. You just create a file inside that block then easily visible that.
Just see the code bellow and test it(create a similar directory with appropiate permission and see the result)
C#
int i = 123;
            string s = "Some string";
            object o = s;
            File.Create(@"D:\Temp\MyTest1.sql").Close();
            try
            {
                // Invalid conversion; o contains a string not an int
                i = (int)o;
            }
            finally
            {
                File.Create(@"D:\Temp\MyTest2.sql").Close();
                Console.Write("i = {0}", i);
            }


After executing code you will see Temp directory contain 2 new files MyTest1.sql and MyTest12sql.

Forgot to mention, You must built it from visual studio and then run it from file system with double click. If you run it from visual studio with debug mode code may not reach there.
 
Share this answer
 
v2
Comments
DileepReddyC 29-Oct-13 8:07am    
Run it from file system with double click means i did not get that..can u please tell me
S. M. Ahasan Habib 29-Oct-13 8:12am    
Sorry for not clear. if you build that project, you will find exe(ex a.exe) file (suppose you create a console app). So then you go that file location(ex d:\projects\a.exe) and double click on that for executing. Let me know still you are confused.
DileepReddyC 29-Oct-13 9:17am    
hi
Test2 is still not created

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900