C# System Exception

In this chapter you will learn:

  1. What is C# System Exception
  2. Common Exception Types
  3. Use the NullReferenceException
  4. Use ArgumentNullException
  5. Use OverflowException
  6. Use DivideByZeroException

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 ww  .  j  a v  a2s  .  co 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;/*from w  ww .ja  va2s  .  com*/

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;// w w w . j  ava  2 s  . c  o  m

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;//www  .  ja v  a  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.

Next chapter...

What you will learn in the next chapter:

  1. How to apply Standard Numeric Format Strings
  2. Example for Standard Numeric Format
Home »
  C# Tutorial »
    C# Language »
      C# Exception
C# Exception
C# Exception classes
C# try Statements and Exceptions
C# catch Clause
C# finally Block
C# throw statement
C# System Exception