Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a small doubt in the Exception handling in C#. In the below code

C#
Try
{

//Throwing Error
}
Catch
{
//Getting Caught
}
Finally
{
int x =3;
}

int x = 5 ;


In Try after throwing an Error which is caught in Catch ,After that Finall Block Executes with Assignment of x=3. and after that next line x=5 is also executed.Program not getting terminated..What might be the reason ?
Posted
Comments
pradiprenushe 28-Aug-12 2:13am    
Are you getting exception in try?
[no name] 28-Aug-12 3:25am    
I bet your code won't even compile. Guess what?

hi read this!!!

The code inside a finally block will get executed regardless of whether or not there is an exception. This comes in very handy when it comes to certain housekeeping (Memory Clean)functions you need to always run like closing connections.

Now, I'm guessing your question is why you should do this:
C#
try
{
    doSomething();
}
catch
{
    catchSomething();
}
finally
{
    alwaysDoThis();
}

When you can do this:
C#
try
{
    doSomething();
}
catch
{
    catchSomething();
}

alwaysDoThis();

The answer is that a lot of times the code inside your catch statement will either rethrow an exception or break out of the current function. With the latter code, the "alwaysDoThis();" call won't execute if the code inside the catch statement issues a return or throws a new exception.

regards
sarva
 
Share this answer
 
v2
Comments
peru j 28-Aug-12 2:14am    
Exactly what i am looking for. you gave me the answer .On what scenarios a new Exception is thrown in a catch Block ? just am trying to know :)
Sarrrva 28-Aug-12 2:31am    
if have time also read this article...
there is good explanation for this

http://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET

http://www.c-sharpcorner.com/UploadFile/rajeshvs/ExceptionHandlinginCSharp11282005051444AM/ExceptionHandlinginCSharp.aspx

this one is Short and sweet explanation:

http://www.go4expert.com/forums/showthread.php?t=3912

regards
sarva
peru j 28-Aug-12 5:04am    
Thanks Sarva. Useful links
Sarrrva 28-Aug-12 5:05am    
Welcome yaar!!!
You have provided a catch block. As a result the program continues normally.
After the error, execution enters the catch block and then runs finally and the line thereafter.


One of the statements mentioned at Exceptions and Exception Handling (C# Programming Guide)[^] is "In many cases, an exception may be thrown not by a method that your code has called directly, but by another method further down in the call stack. When this happens, the CLR will unwind the stack, looking for a method with a catch block for the specific exception type, and it will execute the first such catch block that if finds. If it finds no appropriate catch block anywhere in the call stack, it will terminate the process and display a message to the user.".

If you want to exit the program, re-throw the error from the catch method.
 
Share this answer
 
v2
Case1:If your try block doesnt get any exception code after finally block get executed.
Case2:If your try block doesnt get any exception execution goes to catch if present & then goes to finally if present then terminates.

In you case your try block may not giving any exception. Try to get some exception & debug then you understand.
Here if exception occurs execution does not go to int x = 5 ;
If exception not occurs then execution goes to int x = 5 ;
Refer this
http://forums.asp.net/t/1713477.aspx/1
 
Share this answer
 
v2

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