System exception

In this chapter you will learn:

  1. Commonly Used Exceptions Defined Within the System Namespace
  2. Use the NullReferenceException
  3. Use ArgumentNullException
  4. Use OverflowException
  5. Use DivideByZeroException

Commonly Used Exceptions Defined Within the System Namespace

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.

Use the NullReferenceException

using System;  //from   j  ava2 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.

ArgumentNullException

using System;/*j ava2s  .c o 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.

OverflowException

The following code checks OverflowException for long.

using System;//  ja va 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);
    }

  }

}

DivideByZeroException

Define exception variable in catch statement: DivideByZeroException

using System;// j  av 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. User-Defined Exception Classes
  2. Exception properties
  3. extends Exception to creat your own exception class
Home » C# Tutorial » Exception
Exception
Exception classes
try...catch
catch exceptions
finally
throw exception
System exception
User-Defined Exception Classes