If i will write a piece of code in finaly block, will it execute if there is an exception in try block.

 Posted by Lakhangarg on 2/26/2010 | Category: C# Interview questions | Views: 2384
Answer:

Yes, the code in finally block will execute.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: SurajRane on: 6/18/2010
This is true only if we provide catch block in between.

e.g.
try
{
throw new Exception("Exception");
}
catch
{
}

finally
{
// in this case finally will be executed
}

Now consider following situation

try{
throw new Exception("Exception");
}
finally
{
// this block will not be executed.
// program will be terminated in try block.
}


Login to post response