Manually throw an exception. : Exception Throw : Language Basics C# Examples


C# Examples » Language Basics » Exception Throw »

 

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.");  
    }  
}
    
   
  
   



Output

Before throw.
Exception caught.
After try/catch block.


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Language Basics
» Exception Throw