Custom Exception Program
(1)
//Creating A unchecked Custom Exception
//Custom Exception class
import [Link].*;
class UnderAgeException extends RuntimeException
{
UnderAgeException()//No args constructor of CustomException Class
{
super("Sorry You are Under Age you can't vote");
}
UnderAgeException(String s)//Parametric constructor of CustomException Class
{
super(s);
//Note:Super() provides description to Default exception handler
}
}
//Main code
public class Voting
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter your age::");
int age=[Link]();
if(age<18)
{
throw new UnderAgeException();//For no arg constructor
//or for Parametric pass your msg
//throw new UnderAgeException("Your age is under 18 you are nor eligible for voting");
}
else
{
[Link]("Hey you are eligible to Vote");
}
}
}
(2)Handling Exception with Try catch
import [Link].*;
class UnderAgeException extends RuntimeException
{
UnderAgeException()//No args constructor of CustomException Class
{
super("Sorry You are Under Age you can't vote");
}
UnderAgeException(String s)//Parametric constructor of CustomException Class
{
super(s);
//Note:Super() provides description to Default exception handler
}
}
public class Voting
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter your age::");
int age=[Link]();
try
{
if(age<18)
{
throw new UnderAgeException();//For no arg constructor
//or for Parametric pass your msg
//throw new UnderAgeException("Your age is under 18 you are nor eligible for voting");
}
else
{
[Link]("Hey you are eligible to Vote");
}
}
catch(UnderAgeException e)
{
[Link](e);
[Link]("Exception Handled");
}
}
}
(3)
//Creating A CheckedCustom Exception
//Create custom Exception class
import [Link].*;
//costom Exception class
class UnderAgeException extends Exception //use Exception for Checked exception
{
UnderAgeException()//No args constructor of CustomException Class
{
super("Sorry You are Under Age you can't vote");
}
UnderAgeException(String s)//Parametric constructor of CustomException Class
{
super(s);
//Note:Super() provides description to Default exception handler
}
}
public class Voting
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter your age::");
int age=[Link]();
if(age<18)
{
throw new UnderAgeException();//For no arg constructor
//or for Parametric pass your msg
//throw new UnderAgeException("Your age is under 18 you are nor eligible for voting");
}
else
{
[Link]("Hey you are eligible to Vote");
}
}
}
[Link][Link] error: unreported exception UnderAgeException; must be caught or
declared to be thrown
throw new UnderAgeException();//For no arg constructor
^
(4)//Exception handling with Try catch
import [Link].*;
class UnderAgeException extends Exception
{
UnderAgeException()//No args constructor of CustomException Class
{
super("Sorry You are Under Age you can't vote");
}
UnderAgeException(String s)//Parametric constructor of CustomException Class
{
super(s);
//Note:Super() provides description to Default exception handler
}
}
public class Voting
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter your age::");
int age=[Link]();
try
{
if(age<18)
{
throw new UnderAgeException();//For no arg constructor
//or for Parametric pass your msg
//throw new UnderAgeException("Your age is under 18 you are nor eligible for voting");
}
else
{
[Link]("Hey you are eligible to Vote");
}
}
catch(UnderAgeException e)
{
[Link](e);
[Link]("Exception Handled");
}
}
}