Does the finally block execute even when we have unhandled exceptions?

 Posted by Ddd on 2/8/2011 | Category: C# Interview questions | Views: 3384 | Points: 40
Answer:

Yes, the finally block executes even if the exception is unhandled

Consider this code snippet

using System;


class abc
{
static void Main()
{
try
{
Console.WriteLine("enter");
int a=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("hello"+"---"+a);
}
finally
{
Console.WriteLine("good");
}
}
}


Compile it and run from the Visual sTUDIO Command prompt

put some error value (Example: abc)

The output will consist of the error message and the good string in the
finally block will be displayed.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response