Customized Exception
In this tutorial, we are going to discuss Customized Exception in Java. Based on our programming requirement, sometimes we have to create our exceptions, which are nothing but “Customized Exception.”
E.g
TooYoungException.
TooOldException.
InSufficiantFundsTransfer.
public class TooYoungException extends RuntimeException {
TooYoungException(String s) {
super(s);
}
}
public class TooOldException extends RuntimeException {
TooOldException(String s) {
super(s);
}
}
public class CustomExceptionDemo {
public static void main(String arg[]) {
int age = Integer.parseInt(arg[0]);
if(age > 60) {
throw new TooOldException("Younger age is already over");
} else if(age < 18)
throw new TooYoungException("Please wait same more time");
}
System.out.println("Thanks for register");
}
}
Note
It is recommended to define customized exceptions as unchecked. i.e., our custom exceptions class should extends RuntimeException either directly or indirectly.
Customized Exception