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
ww___w___.__j__a_v__a___2___s__._c__o___m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.