ArgumentException is thrown when one of the arguments provided to a method is not valid. : Buildin Exceptions « Development Class « C# / C Sharp






ArgumentException is thrown when one of the arguments provided to a method is not valid.

 

using System;

public sealed class App 
{
    static void Main() 
    {
        Console.WriteLine("10 divided by 2 is {0}", DivideByTwo(10));
        try 
        {
             Console.WriteLine("7 divided by 2 is {0}", DivideByTwo(7));
        }
        catch (ArgumentException)
        {
            Console.WriteLine("7 is not divided by 2 integrally.");
        }
    }

    static int DivideByTwo(int num) 
    {
        if ((num & 1) == 1)
            throw new ArgumentException("Number must be even", "num");
        return num / 2;
    }
}

   
  








Related examples in the same category

1.Throw and Catch an IO exception because the file zxcvb.data doesn't exist
2.OutOfMemoryException Exception
3.StackOverflowException Exception
4.NullReferenceException Exception
5.InvalidCastException Exception
6.IndexOutOfRangeException Exception
7.OverflowException Exception
8.Throw Index Out Of Range Exception
9.Throw Null reference Exception
10.Throw Invalid Operation Exception
11.Argument Null Exception
12.Throw Argument Out Of Range Exception
13.OverflowException