Exception throw and catch

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/

 using System;

 namespace ExceptionHandling
 {
    public class TesterExceptionHandling2
    {
       static void Main()
       {
           Console.WriteLine("Enter Main...");
           TesterExceptionHandling2 t = new TesterExceptionHandling2();
           t.Run();
           Console.WriteLine("Exit Main...");
       }
       public void Run()
       {
           Console.WriteLine("Enter Run...");
           Func1();
           Console.WriteLine("Exit Run...");
       }


        public void Func1()
        {
            Console.WriteLine("Enter Func1...");
            Func2();
            Console.WriteLine("Exit Func1...");
        }

        public void Func2()
        {
            Console.WriteLine("Enter Func2...");
            try
            {
                Console.WriteLine("Entering try block...");
                throw new System.Exception();
                Console.WriteLine("Exiting try block...");
            }
            catch
            {
                Console.WriteLine("Exception caught and handled!");
            }
            Console.WriteLine("Exit Func2...");
        }
    }
 }