If you want to execute the sequential statement even if a exception occur, use try and catch block and handle the exception or even do not write anything in catch block, it will automatically execute the next statement.
The below sample program is example of executing the next statement after the exception.
using System;
namespace CSharpInterview
{
class Programs
{
static void Main(string[] args)
{
Console.WriteLine("Example to execute the code after exception");
Console.WriteLine("");
Console.WriteLine("Before Exception");
try
{
throw new NullReferenceException();
}
catch (Exception ex)
{
Console.WriteLine("Exception Handled: {0}", ex.Message);
}
Console.WriteLine("After Exception");
Console.ReadLine();
}
}
}
No comments:
Post a Comment