Answers for "c# types of exception negative number"

C#
0

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);
  }
}
}
Posted by: Guest on May-04-2021

C# Answers by Framework

Browse Popular Code Answers by Language