c# types of exception negative number code example
Example: c# types of exception negative number
using System;
class NegativeNumberNotAllowed: Exception
{
public NegativeNumberNotAllowed(string message): base(message)
{
}
}
class Test
{
static void CheckForNegative(int number)
{
if(number < 0)
{
throw new NegativeNumberNotAllowed("Negative number is not allowed");
}
}
static void Main(string[] args)
{
try
{
CheckForNegative(-10);
}
catch(NegativeNumberNotAllowed e)
{
Console.WriteLine(e);
}
}
}