Java exceptions is a good mechanism to respond to unexpected conditions in our program. But sometimes a wrong implementation can cause that our program become a nightmare at production time. For instance, it can overflow our error logs with wrong information making impossible to find the problem's source.
In this post I want to show a recap of what to do and what to avoid while implementing Java Exceptions, and how to deal with the most common exceptions types.
Lets start with the rules.
Following these three rules will help you to implement exceptions in a easier way. The rules are:
In this post I want to show a recap of what to do and what to avoid while implementing Java Exceptions, and how to deal with the most common exceptions types.
Lets start with the rules.
Basic rules for exceptions.
Following these three rules will help you to implement exceptions in a easier way. The rules are:
- Be specific
- Throw early
- Catch late
What to avoid.
In this other article you'll find a list of anti-patterns for exceptions. In summary these anti-patterns are:- Log and throw.
- Throwing just java.lang.Exception.
- Throwing the kitchen sink.
- Catching just java.lang.Exception
- Destructive wrapping.
- Log and return null.
- Catch and ignore.
- Throw from within finally block.
- Multi-line log messages.
- Unsupported operation returning null.
- Ignoring java.lang.InterruptedException
- Relying on getCause method.
List of common exceptions.
ArrayIndexOutOfBoundsException and IndexOutOfBoundsException.
ArrayIndexOutOfBoundsException is thrown when a portion of code tries to accessan array either at a position less than 0 or at a position greater than or equal to its length.
IndexOutOfBoundsException is the same as ArrayIndexOutOfBoundsException but applicable to collections.
You can avoid these exceptions from being thrown if you check the index position as follow:
if(index > 0 && index < myList.size()){
//Access list element.
}
ClassCastException.
ClassCastException extends from RunTimeException, means that it is an unchecked exception. This exception is thrown when an object fails an IS-A test. You can use the instanceof operator before casting an object to verify whether it can be cast or not.
IllegalArgumentException.
As the name of this exceptions specifies, IllegalArgumentException is thrown when a method has passed illegal or inappropriate arguments.
This Exception is usually used to validate the arguments that are passed to a method.
IllegalStateException.
IllegalStateException is used more as a signal. It notifies that a method has been invoked at an illegal or inappropriate time. For more information refer to the API documentation.
You can thrown this exception in your code to signal that a method being requested for execution can't be called for the current state of an object.
NullPointerException.
This is the most common exception of all. This exception is thrown if you try to access a method or a variable with a null value.
Validating if the reference we are about to use is not null is a way to avoid this exception.
Validating if the reference we are about to use is not null is a way to avoid this exception.
NumberFormatException.
This exception is thrown when a program has tried to convert a string with an inappropriate format to one of the numeric types.