Write a program to implement the concept of Exception Handling using by creating user defined exceptions.

 

import java.lang.Exception;
class EvenNumberException extends Exception
{
EvenNumberException(String emsg)
{
super(emsg);
}
}
class testexcep
{
public static void main(String args[])
{
int num=5,r;
try
{
r=num%2;
if(r!=0)
{
throw new EvenNumberException("Number is not even");
}
}
catch(EvenNumberException e)
{
System.out.println("Caught User defined Exception");
System.out.println(e.getMessage());
}
}
}

 

 

Output:
C:\>java testexcep
Caught User defined Exception
Number is not even
C:\>