Use the NullReferenceException. : Predefined Exception « Language Basics « C# / CSharp Tutorial






using System;  
  
class MainClass {   
  public static void Main() {   

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








1.23.Predefined Exception
1.23.1.Use the NullReferenceException.
1.23.2.Throwing Exceptions: ArgumentNullException
1.23.3.Handle ArgumentOutOfRangeException
1.23.4.Check OverflowException for long
1.23.5.Generate an index out-of-bounds exception
1.23.6.Define exception variable in catch statement: DivideByZeroException