Home > Interview Questions > C#.NET > Will finaly block gets executed if exception has not occured in C#?

Will finaly block gets executed if exception has not occured in C#?

by Thamil   on 29/09/2015   Category: C#.NET   |  Level: Intermediate   |  Views: 599    |  Points: 25     |  Gold 


Yes, The finally block will be executed always when control leaves from try catch block and it's get executed irrespective of the exception has occurred or not.

try
{
//yourlogic
}
catch
{
//catchexception
}
finally
{
//Alwaysexecute;
}

Additional points:
-Finally block would be used to clean up the resources that are allocated in a try block.
-Suppose if catch block rethrows the exception, then finally block would execute after the catch block.
-If any assignment of return value in finally block is no effect. See the below sample
private int TestMethod()
{
int retval = 10;
try
{
return retval;
}
finally
{
retval = 5;
}
}


Above method will return 10 and not 5.


« What is the entry point for C# console application?
» What are the types of comment in C# with examples?
Post Question  |  Question Home

Recent Posts

User Responses


No response found, be the first to review this question.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts