catch statement

The exception type is specified in the catch statement.

The exception type can be System.Exception or its subclasses.

The following code catches each possible exception type one by one using catch statement.


using System;

class Test
{
    static void Main(string[] args)
    {
        try
        {
            byte b = byte.Parse(args[0]); Console.WriteLine(b);
        }
        catch (IndexOutOfRangeException ex)
        {
            Console.WriteLine("Please provide at least one argument");
        }
        catch (FormatException ex)
        {
            Console.WriteLine("That's not a number!");
        }
        catch (OverflowException ex)
        {
            Console.WriteLine("You've given me more than a byte!");
        }
    }
}

The output:


Please provide at least one argument
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.