Mega Code Archive

 
Categories / C# Tutorial / Language Basics
 

Manually throw an exception

The general form is: throw exceptOb; The exceptOb must be an object of an exception class derived from Exception. using System;    class MainClass {    public static void Main() {      try {        Console.WriteLine("Before throw.");        throw new DivideByZeroException();      }      catch (DivideByZeroException) {        // catch the exception        Console.WriteLine("Exception caught.");      }      Console.WriteLine("After try/catch block.");    }  } Before throw. Exception caught. After try/catch block.