Stack unwinding and Exception class properties. - CSharp Custom Type

CSharp examples for Custom Type:Exception

Description

Stack unwinding and Exception class properties.

Demo Code



using System;//from   w ww .  j a v  a2s .c  om

class Properties
{
   static void Main()
   {
      try
      {
         Method1();
      }
      catch (Exception exceptionParameter)
      {
         Console.WriteLine("exceptionParameter.ToString: \n" + exceptionParameter);
         Console.WriteLine("\nexceptionParameter.Message: \n" + exceptionParameter.Message);
         Console.WriteLine("\nexceptionParameter.StackTrace: \n" + exceptionParameter.StackTrace);
         Console.WriteLine("\nexceptionParameter.InnerException: \n" + exceptionParameter.InnerException);
      }
   }

   // calls Method2
   static void Method1()
   {
      Method2();
   }

   // calls Method3
   static void Method2()
   {
      Method3();
   }

   // throws an Exception containing an InnerException
   static void Method3()
   {
      // attempt to convert string to int
      try
      {
         int.Parse("Not an integer");
      }
      catch (FormatException formatExceptionParameter)
      {
         // wrap FormatException in new Exception
         throw new Exception("Exception occurred in Method3",
            formatExceptionParameter);
      }
   }
}

Result


Related Tutorials