0% found this document useful (0 votes)
15 views4 pages

Custom Exception

The document presents a Java program that demonstrates the creation and handling of custom exceptions related to age verification for voting. It includes both unchecked and checked custom exceptions, with examples of how to throw and catch these exceptions using try-catch blocks. The program prompts the user for their age and throws an exception if they are under 18, providing appropriate messages for both eligible and ineligible voters.

Uploaded by

kushsevak9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Custom Exception

The document presents a Java program that demonstrates the creation and handling of custom exceptions related to age verification for voting. It includes both unchecked and checked custom exceptions, with examples of how to throw and catch these exceptions using try-catch blocks. The program prompts the user for their age and throws an exception if they are under 18, providing appropriate messages for both eligible and ineligible voters.

Uploaded by

kushsevak9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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");
}
}
}

You might also like