Sponsored Links
Ad by Google
In this tutorial, I will show you how to create custom exception. Although I have already posted about Exception in my previous Exception Tutorial post. And we all know Exception is very important and actively used feature in daily programming practice.
Sometimes interviewer also asked you about custom exception in java, and ask to write an example of custom exception, so this tutorial is important in both purpose to use or to be in java interview, this question is popular in java interview as like as Why String is immutable in java.
Although I have already posted Top 20 Exception Handling interview questions and 15 core java written interview questions
How to create custom Exception
You can create custom exception by either extending -
InvalidAccountException .java
Unchecked Exception Example -
UncheckedCustomException .java
Custom Exception usage Example -
CustomExceptionUsageExample.java
validateAccount(string) method throwing an InvalidAccountException, So it's responsibility of the caller of validateAccount method to handle the InvalidAccountException.That's why in main method we are surrounded with try catch block while calling validateAccount method at line number 19.
But UncheckedCustomException is a unchecked exception and created with extending Runtime Exception, that is why isAccountExist method is throwing an UncheckedCustomException but caller of isAccountExist method no need to handle the UncheckedCustomException, in main method we are simply calling the isAccountExist method at line number 26 outside of the try-catch block.
For more please read what is difference between Checked and Unchecked Exception
OUT PUT of the CustomExceptionUsageExample.java
com.javamakeuse.poc.InvalidAccountException: Invalid accountlog error message - com.javamakeuse.poc.InvalidAccountException: Invalid account at com.javamakeuse.poc.CustomExceptionUsageExample.validateAccount(CustomExceptionUsageExample.java:12)
at com.javamakeuse.poc.CustomExceptionUsageExample.main(CustomExceptionUsageExample.java:19)
That's it about How to create custom exception in java.
Sometimes interviewer also asked you about custom exception in java, and ask to write an example of custom exception, so this tutorial is important in both purpose to use or to be in java interview, this question is popular in java interview as like as Why String is immutable in java.
Although I have already posted Top 20 Exception Handling interview questions and 15 core java written interview questions
How to create custom Exception
You can create custom exception by either extending -
- java.lang.Exception (Checked Exception)
- java.lang.RuntimeException (Unchecked Exception)
- But before creating any custom exception think twice, Is it really required custom exception, are built in exceptions will not helping anymore for you.
- Once you decided to create custom exception think twice, which types of custom exception required for your methods, Is it Checked Exception or it would be Unchecked Exception? you can read more about Checked Exception vs Unchecked Exception, which will help you to take decision on the second point.
InvalidAccountException .java
package com.javamakeuse.poc; public class InvalidAccountException extends Exception { /** * */ private static final long serialVersionUID = 1L; public InvalidAccountException() { super(); } public InvalidAccountException(String message) { super(message); } public InvalidAccountException(Exception cause) { super(cause); } public InvalidAccountException(String message, Exception cause) { super(message, cause); // TODO: you can log here cause } // This constructor is from jdk 7 onwards. public InvalidAccountException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); // If writableStackTrace is false than, stackTrace should not be write // able } }
Unchecked Exception Example -
UncheckedCustomException .java
package com.javamakeuse.poc; public class UncheckedCustomException extends RuntimeException { /** * */ private static final long serialVersionUID = 1L; public UncheckedCustomException() { // TODO Auto-generated constructor stub } public UncheckedCustomException(String message) { super(message); // TODO Auto-generated constructor stub } public UncheckedCustomException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } public UncheckedCustomException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public UncheckedCustomException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); // TODO Auto-generated constructor stub } }Although you can keep constructors as per your requirement, it's not mandatory, it's up to your requirement.
Custom Exception usage Example -
CustomExceptionUsageExample.java
package com.javamakeuse.poc; public class CustomExceptionUsageExample { public static boolean validateAccount(String account) throws InvalidAccountException { boolean done = false; if (account != null && !account.trim().isEmpty()) { // TODO: do other stuffs } else { throw new InvalidAccountException("Invalid account"); } return done; } public static void main(String[] args) { try { System.out.println(validateAccount("")); } catch (InvalidAccountException e) { System.out.println("log error message - " + e); e.printStackTrace(); } // Not mandatory to handled the runtime exceptions isAccountExist(""); } public static boolean isAccountExist(String account) throws UncheckedCustomException { // Return true, if account exist otherwise false. return false; } }Points to note - In above usage example, InvalidAccountException is a Checked Exception, and
validateAccount(string) method throwing an InvalidAccountException, So it's responsibility of the caller of validateAccount method to handle the InvalidAccountException.That's why in main method we are surrounded with try catch block while calling validateAccount method at line number 19.
But UncheckedCustomException is a unchecked exception and created with extending Runtime Exception, that is why isAccountExist method is throwing an UncheckedCustomException but caller of isAccountExist method no need to handle the UncheckedCustomException, in main method we are simply calling the isAccountExist method at line number 26 outside of the try-catch block.
For more please read what is difference between Checked and Unchecked Exception
OUT PUT of the CustomExceptionUsageExample.java
com.javamakeuse.poc.InvalidAccountException: Invalid accountlog error message - com.javamakeuse.poc.InvalidAccountException: Invalid account at com.javamakeuse.poc.CustomExceptionUsageExample.validateAccount(CustomExceptionUsageExample.java:12)
at com.javamakeuse.poc.CustomExceptionUsageExample.main(CustomExceptionUsageExample.java:19)
That's it about How to create custom exception in java.
Sponsored Links
0 comments:
Post a Comment