Java Program that Implement Custom Exception

Write a Java Program that Implement Custom Exception:-

import java.util.*;
class MyException extends Exception
{
private int e;
MyException (int a )
{
e=a;
}
public String toString()
{
return ("Error in entry"+e);
}
}
public class mine
{ public void compute(int a) throws MyException
{
int age=a;
if(age>150)
throw new MyException (age);
System.out.println("COrrect age");
}
public static void main(String args[])
{
mine m=new mine();
try{
m.compute(1);
m.compute(789);
}
catch(MyException e)
{
System.out.println(e);
}
}
}

Related Posts