Sponsored Ad

Saturday, May 12, 2012

Interview Questions: Handle the Exception and Execute Sequential Statement after the Exception in C#

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.

Interview Questions: Handle the Exception and Execute Sequential Statement after the Exception in C#

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

Sponsored Ad

Followers

Follow Us