Throw exception

To throw exception we can use the throw statement.


using System;
using System.IO;

class Test
{
    static void Display(string name)
    {
        if (name == null)
            throw new ArgumentNullException("name");

        Console.WriteLine(name);
    }

    static void Main()
    {
        try { Display(null); }
        catch (ArgumentNullException ex)
        {
            Console.WriteLine("Caught the exception");
        }
    }
}

The output:


Caught the exception
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.