Check the code running sequence during try catch statement - CSharp Custom Type

CSharp examples for Custom Type:try catch finally

Description

Check the code running sequence during try catch statement

Demo Code

using System;/*w ww. j av  a 2s .  c o m*/
class MyClass
{
   public static void Main()
   {
      Console.WriteLine("Entering MyClass.Main");
      YourClass yourObject = new YourClass();
      yourObject.Method1();
      Console.WriteLine("Leaving MyClass.Main");
   }
}
class YourClass
{
   public void Method1()
   {
      Console.WriteLine("Entering YourClass.Method1");
      try
      {
         Console.WriteLine("Entering the try block of Method1");
         Method2();
         Console.WriteLine("Leaving the try block of Method1");
      }
      catch(DivideByZeroException exObj)
      {
         Console.WriteLine("Entering catch block of Method1");
         Console.WriteLine("Exception: " + exObj.Message);
         Console.WriteLine("Exception was generated in Method2");
         Console.WriteLine("Leaving catch block of Method1");
      }
      Console.WriteLine("Leaving YourClass.Method1");
   }
   public void Method2()
   {
      Console.WriteLine("Entering YourClass.Method2");
      int myInt = 0;
      int yourInt;
      //Dividing by zero
      yourInt = 10 / myInt;
      Console.WriteLine("Leaving YourClass.Method2");
   }
}

Result


Related Tutorials