Java Customized Exceptions
Sometimes to fulfill the programming requirement we can define our own exceptions such type of exceptions are called customized exceptions.
Let's take an example of customized exceptions.
import java.util.*;
class TooYoungException extends RuntimeException
{
TooYoungException (String s)
{
super(s);
}
}
class TooOldException extends RuntimeException
{
TooOldException (String s)
{
super(s);
}
}
class CustomizeException
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the age ");
int age=sc.nextInt();
if(age>60)
{
throw new TooOldException("Your age is already crossed the marriage age...
try In next birth... ");
}
else if(age<18)
{
throw new TooYoungException("Please wait some time...
You will get your best match soon... ");
}
else
{
System.out.println("Congrats!!!You are eligible for marriage..
you will get details of your match by email!!");
}
}
}
Output is:-
No comments:
Post a Comment
Be the first to comment!
Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.
Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
Regards,
JavaByChetan
Back To Home