ArgumentOutOfRangeException in .NET
In this article we will explain how we can handle the ArgumentOutOfRangeException exception in .NET.
Introduction
Exception handling is very common in programs. Exceptions are used to indicate that an error has occurred while running the program. An ArgumentOutOfRangeException indicates that an argument you passed to a method is out of numeric range. Computers can't handle this situation without using exception handling so its gives an error or Exception. This exception is called ArgumentOutOfRangeException.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Dividebyzero
{
class Program
{
static int N1;
public static void Ma(int N1)
{
if (N1 < 0)
{
// throw an argument zero.
throw new ArgumentOutOfRangeException("Age Cannot Be Negative ");
}
}
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter the age");
N1 = Convert.ToInt32(Console.ReadLine());
Ma(N1);
Console.WriteLine("Your age is {0}", N1);
}
catch (Exception ex)
{
Console.WriteLine( ex);
Console.WriteLine("HelpLink = {0}", ex.HelpLink);
Console.WriteLine("Message = {0}", ex.Message);
Console.WriteLine("Source = {0}", ex.Source);
Console.WriteLine("StackTrace = {0}", ex.StackTrace);
Console.WriteLine("TargetSite = {0}", ex.TargetSite);
}
finally
{
Console.WriteLine("Task completed");
Console.ReadLine();
}
}
}
}
The output of following program