What is Exception Handling
Posted By : Harshit Verma | 22-May-2018
What is Exception?
Exceptions are defined as "condition that stops the free flow of code and interrupts the normal flow of the program.
The exception is classified into two types:-
1. Checked exception.
2.Unchecked exception.
What is Exception handling?
Exception handling is defined as a mechanism that is used to handle the error generating code and
The following are the best practices to handle the Exceptions in Java :-
1. Using the resource along with the try and a
public void doNotCloseResourceInTry() {
FileInputStream inputStream = null;
try {
File file = new File("./tmp.txt");
inputStream = new FileInputStream(file);
// use the inputStream to read a file
// do NOT do this
inputStream.close();
} catch (FileNotFoundException e) {
log.error(e);
} catch (IOException e) {
log.error(e);
}
}
You can use this instead of the code at upper side:-
public void closeResourceInFinally() {
FileInputStream inputStream = null;
try {
File file = new File("./tmp.txt");
inputStream = new FileInputStream(file);
// use the inputStream to read a file
} catch (FileNotFoundException e) {
log.error(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log.error(e);
}
}
}
}
2. Always try to prefer the specific exceptions when you are going to handle the exceptions:- If you are using the specific details along with the exception, it will be helped much to others when any other person is using your code or reading your code.
3. Always document the exception in your JavaDoc that you specifically used:-For a good practice, you have to declare the exception in the JavaDoc. The @throws is used to declare the exceptions.
/**
* This method does some stuff ...
*
* @param input
* @throws MyBusinessException if ... happens
*/
public void doSomething(String input) throws MyBusinessException { ... }
4. Whenever you are throwing an exception, the descriptive messages are considering rather than short messages:- This says that if the throws exception name does not provide the sufficient information about the exception, then it's a good practice to provide a declarative message along with the exception.
5. Catch the most specific exception first:- Always try to catch the specific exception else the exception handling procedure will check all of the exception class for getting the specific exception, hence try to avoid to catch the exception class instead of that you have to catch the specific exception.
6. Do not catch the throwable as it considered as a bad practice:- Do not use to catch the Throwable, as it's not a good practice. As throwable is the parent class, always try to catch the specific exception.
public void doNotCatchThrowable() {
try {
// do something
} catch (Throwable t) {
// don't do this!
}
}
7. Do not ignore the exceptions, always try to handle them or throws them:- Always try to handle the exception at your's best, else this may cause issues in the future. If you are putting some validation in your code and due to any reason the validation fails, that cause interruption of the normal flow of code, hence always try to handle the exceptions instead of ignoring them.
8. Do not throw the exception, just after log having an exception as it considers bad practice:-
try {
new Long("xyz");
} catch (NumberFormatException e) {
log.error(e);
throw e;
}
9. Wrap the exception without consuming:- When you are dealing with the exception it's really a good practice to catch the specific exception and wrap up with the custom one.
try {
// do something
} catch (NumberFormatException e) {
throw new MyBusinessException("message that describing the error.", e);
}
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Harshit Verma
Harshit is a bright Web Developer with expertise in Java and Spring framework and ORM tools Hibernate.