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