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 setup the communication medium between the code at the same time. Exception handling in Java is an interesting topic, but people find it quite difficult.

 

The following are the best practices to handle the Exceptions in Java :-

 

1. Using the resource along with the try and a finally block for the cleanup actions:- If you are using the try with the resource then, using the finally is a good practice. In a try block we are keeping the code that may or may not generating the exceptions, hence if somehow the try block is running the connection at first is established, but due to the occurring of the exception the connection close method is not executed, that's why we have to used finally block for the cleaning actions.

 

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);
	}
}

About Author

Author Image
Harshit Verma

Harshit is a bright Web Developer with expertise in Java and Spring framework and ORM tools Hibernate.

Request for Proposal

Name is required

Comment is required

Sending message..