In this post, we will learn how to create the custom exception in Java. To achieve this we can create a Java custom exception class that can
extends Exception class.
Below code is a sample example of custom or user defined exception class. In this example, if entered age value is less than or equal to 0 or greater than 100 then it will throw the exception with user defined message at the client side.
In the below class, the ageValidate(int age) method will throw the Runtime exception i.e InvalidAgeException when the entered age is less than or equal to 0 or greater than 100. We can provide user understandable message at the client side so user can easily understand the exception.
Output of the above code: -
com.practice.InvalidAgeException: Entered age is not a valid age at com.practice.CustomExceptionDemo.validateAge(CustomExceptionDemo.java:12)
at com.practice.CustomExceptionDemo.main(CustomExceptionDemo.java:23)
The base Exception class has the following constructors, we used one constructor in the above example.
1) Exception()
2) Exception (String message)
3) Exception (String message, Throwable cause)
4) Exception (Throwable cause)
Related Post: --
Exception Handling Interview questions and answers
How to Create Custom Immutable Class with mutable object references in Java?
Factory Design Pattern in Java
Difference between final,finalize and finally in Java
Below code is a sample example of custom or user defined exception class. In this example, if entered age value is less than or equal to 0 or greater than 100 then it will throw the exception with user defined message at the client side.
public class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
In the below class, the ageValidate(int age) method will throw the Runtime exception i.e InvalidAgeException when the entered age is less than or equal to 0 or greater than 100. We can provide user understandable message at the client side so user can easily understand the exception.
public class CustomExceptionDemo {
public int validateAge(int age) {
if (age <=0 || age > 100) {
try {
throw new InvalidAgeException("Entered age is not a valid age");
} catch (InvalidAgeException ex) {
ex.printStackTrace();
}
}
return age;
}
public static void main (String[] args){
CustomExceptionDemo demo = new ExceptionDemo();
//int age = demo.validateAge(36);
int age1 = demo.validateAge(120); // here it will throw exception.
}
}
Output of the above code: -
com.practice.InvalidAgeException: Entered age is not a valid age at com.practice.CustomExceptionDemo.validateAge(CustomExceptionDemo.java:12)
at com.practice.CustomExceptionDemo.main(CustomExceptionDemo.java:23)
The base Exception class has the following constructors, we used one constructor in the above example.
1) Exception()
2) Exception (String message)
3) Exception (String message, Throwable cause)
4) Exception (Throwable cause)
Related Post: --
Exception Handling Interview questions and answers
How to Create Custom Immutable Class with mutable object references in Java?
Factory Design Pattern in Java
Difference between final,finalize and finally in Java
Nice post thanks for sharing...
ReplyDeletenice blog........ i got some details about exception thank you so much
ReplyDeletejava j2ee training course contents | java j2ee training institutes in chennai | java j2ee training | java training in chennai velachery
Thanks for sharing this informative content which provided me the required information about the latest technology.you can also visit: Python and Cloud Computing: Deploying Applications
ReplyDelete