C# System Exception

Description

The most important properties of System.Exception are the following:

  • StackTrace - A string representing all the methods that are called from the origin of the exception to the catch block.
  • Message - A string with a description of the error.
  • InnerException - The inner exception (if any) that caused the outer exception. This, itself, may have another InnerException.

Common Exception Types

Exception Meaning
ArrayTypeMismatchException Type is incompatible with the type of the array.
DivideByZeroException Division by zero attempted.
IndexOutOfRangeException Array index is out of bounds.
InvalidCastException A runtime cast is invalid.
OutOfMemoryException Insufficient free memory exists.
OverflowException An arithmetic overflow occurred.
NullReferenceException An attempt was made to operate on a null reference?that is, a reference that does not refer to an object.
StackOverflowException The stack was Overflow.

As a general rule, exceptions defined by you should be derived from ApplicationException since this is the hierarchy reserved for application- related exceptions.

Example


using System;  //from w  w w.  j a v  a2 s.  c o m
  
class MainClass {   
  public static void Main() {   

    try { 
      string str = null;
      Console.WriteLine(str.ToString());
      
    } catch (NullReferenceException) { 
      Console.WriteLine("NullReferenceException!"); 
      Console.WriteLine("fixing...\n"); 
 
    } 
 
  }  
}

The code above generates the following result.

Example 2


using System;/*w  ww .jav a2 s.co  m*/

class MainClass
{
   static void Main()
   {
      string arg = null;
      
      try
      {
         if (arg == null)
         {
            ArgumentNullException MyEx = new ArgumentNullException();
            throw MyEx;
         }
      }
      catch (ArgumentNullException e)
      {
         Console.WriteLine("Message: {0}", e.Message);
      }
   }
}

The code above generates the following result.

Exception 3

The following code checks OverflowException for long.


using System;//from w ww .  jav a 2s .com

class MainClass
{
  static void Main(string[] args)
  {
    int MyInt = 12345000;
    long MyLong = MyInt;
    
    try
    {
      long c = checked(MyLong * 5000000);
    }
    catch (OverflowException e)
    {
      Console.WriteLine(e);
    }

  }

}

The code above generates the following result.

Example 4

Define exception variable in catch statement: DivideByZeroException.


using System;/*  w  w w . ja va  2  s .co m*/

class MainClass
{
   static void Main()
   {
      try
      {
         int y = 0;
         y = 10/y; 
      } catch (DivideByZeroException e) {
         Console.WriteLine("Message: {0}", e.Message);
         Console.WriteLine("Source: {0}", e.Source);
         Console.WriteLine("Stack: {0}", e.StackTrace);
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    C# Language »




C# Hello World
C# Operators
C# Statements
C# Exception