Wednesday, 28 March 2018

CUSTOM EXCEPTION

WHAT IS CUSTOM EXCEPTION ?

          Custom exceptions can be used to add meaningful, and user-friendly information  to exceptions when errors occur while your program is running.
Exception Class is a base class of the all exceptions.

PROGRAM:(CustomExceptionEg.cs)

using System;
using System.Collections;
namespace Trainning
{
class CustomExceptionEg:Exception           //Inherit the Exception class  for create the custom                                                                                                 exception
{
public CustomExceptionEg(string Message):base(Message)
{}

}
class program
{
public static void Main(string []args)
{
try
{
int age=24;
if (age>18)
throw new CustomExceptionEg("age is invalid"); 
else
Console.WriteLine("u are welcome");

}
catch(Exception e)
{
Console.WriteLine(e.Message);
}

}
}

}

OUTPUT:

D:\csharp>CustomExceptionEg

age is invalid

PROGRAM EXPLANATION:
   The above Program is a simple example for  Custom Exception,And the program condition is check the given age value is greater than 18 the program run regularly other wise it  throw the exception .






No comments:

Post a Comment