throw keyword:
The throw keyword is used to explictily throw an exception.We can throw either checked or uncheked exception. The throw keyword is mainly used to throw custom exception.
Example of throw keyword:
In this example, we have created the checkAge method that takes integer value as a parameter.If the age is less than 18, we are throwing the ArithmeticException otherwise print a message You are not minor.
class Test{
static void checkAge(int age){
if(age<18)
{
throw new ArithmeticException("you are minor");
}
else {
System.out.println("you are not minor");
}
}
public static void main(String []args){
checkAge(15);
System.out.println("rest of the code");
}
}
Output:
Exception in thread main java.lang.ArithmeticException:you are minor //if age=15
you are not minor //if age=20
rest of the code
Custom Exception :
If you are creating your own Exception that is known as custom exception or user-defined exception.
For Example:
save as InvalidAgeException.java
class InvalidAgeException extends Exception{
InvalidAgeException(String s){
}
}
save as Test.java
class Test{
static void checkAge(int age) throws InvalidAgeException {
if(age<18)
throw new InvalidAgeException("you are minor");
else
System.out.println("you are not minor");
}
public static void main(String []args){
try{
checkAge(15);
}catch(Exception ex){
System.out.println("Exception occured... "+ex);
}
System.out.println("rest of the code");
}
}
Output: Exception occured... InvalidAgeException:you are minor
rest of the code
The throw keyword is used to explictily throw an exception.We can throw either checked or uncheked exception. The throw keyword is mainly used to throw custom exception.
Example of throw keyword:
In this example, we have created the checkAge method that takes integer value as a parameter.If the age is less than 18, we are throwing the ArithmeticException otherwise print a message You are not minor.
class Test{
static void checkAge(int age){
if(age<18)
{
throw new ArithmeticException("you are minor");
}
else {
System.out.println("you are not minor");
}
}
public static void main(String []args){
checkAge(15);
System.out.println("rest of the code");
}
}
Output:
Exception in thread main java.lang.ArithmeticException:you are minor //if age=15
you are not minor //if age=20
rest of the code
Custom Exception :
If you are creating your own Exception that is known as custom exception or user-defined exception.
For Example:
save as InvalidAgeException.java
class InvalidAgeException extends Exception{
InvalidAgeException(String s){
}
}
save as Test.java
class Test{
static void checkAge(int age) throws InvalidAgeException {
if(age<18)
throw new InvalidAgeException("you are minor");
else
System.out.println("you are not minor");
}
public static void main(String []args){
try{
checkAge(15);
}catch(Exception ex){
System.out.println("Exception occured... "+ex);
}
System.out.println("rest of the code");
}
}
Output: Exception occured... InvalidAgeException:you are minor
rest of the code
No comments:
Post a Comment