Exception Handling in Java

‘Exceptions’, handling them, declaring them is yet another objective of the OCPJP exam.  We will be dealing with ‘Exceptions’ and more particularly ‘Handling Exceptions’ in this post.

According to the Oracle website, “An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program’s instructions.” (Java Tutorials) When the normal flow is disrupted and an exception is thrown, the exception is handled by means of a “try-catch” block.

Types of exceptions:
There are three types of exceptions – Checked exceptions, Errors and Runtime exceptions. This is the hierarchy of exceptions:

ExceptionHandling1

 

Checked exceptions:
All exceptions are “checked exceptions” except those under “java.lang.Error” and “java.lang.RuntimeException” and their sub-classes. One common example of a checked exception is when a user tries to open a file that is not present – an exception is thrown. Other examples of checked exceptions are hardware problems and common bugs. It is up to the programmer to handle these exceptions appropriately.

Errors:
Errors are unchecked exceptions. Errors are conditions that usually a compiler is not expected to recover from.  These conditions occur, external to the application.  Hence, errors are not caught by the “try-catch” constructs. Thread death, IO errors are some examples of ‘Errors’.

RuntimeException:
Runtime exceptions are again unchecked exceptions. They are similar to errors – in that the application is not expected to recover from Runtime exceptions. The only difference is that runtime exceptions occur within the application. Some examples of runtime exceptions are ‘ArithmeticException’, ‘NullPointerException’ etc. All sub-classes of the ‘java.lang.RuntimeException’ fall under this. It is not wrong to handle unchecked exceptions – however the compiler doesn’t expect this.

Handling exceptions:
Having seen the different types of exceptions, we now see how an exception is handled. All exceptions are handled via the ‘try-catch’ block. The list below illustrates the syntax of a “try-catch” block.

try{
//1. some code here that might raise an exception
}

2. catch(specificException e){
// do appropriate action here
}
catch(GeneralException e){
// do appropriate action here
}
finally{
//clean up code is here
}

If the code in the “try” block at line 1 produces a ‘specificException’, it will be propagated to line 2. The exception will be handled in the ‘catch’ block and the control will then move to the ‘finally’ block. Clean up code is executed in the “finally” block and control moves out of the “try-catch” structure.
Even if there are no exceptions at line 1, control will first move to the ‘finally’ block and then leave the “try-catch” structure.

Here are some key points regarding the ‘finally’ block:

  1. ‘finally’ block almost always runs.
    If an exception is thrown, it runs. If an exception is not thrown, it runs. If an exception is caught, it runs. If an exception is not caught, it runs! (Kathy Sierra)
  2. ‘finally’ clauses are not required.

Next, we see how specific exceptions and general exceptions must be stated.
Now consider the following piece of code:

ExceptionHandling2

Here, since we try to access the fourth element of the array, an ‘ArrayIndexOutOfBoundsException’ is thrown.  Notice, that, the ‘ArrayIndexOutOfBoundsException’ is stated before ‘IndexOutOfBoundsException’.  In other words, specific exceptions are declared before the general one.

If the code was modified and the “IndexOutOfBoundsException” was placed before the “ArrayIndexOutOfBoundsException”, a compiler error would result.

ExceptionHandling3

 

We dealt with the different types of exceptions and ways of handling them in this post. We will continue our discussion of exceptions in the next post.

Bibliography
Java Tutorials. (n.d.). Retrieved Nov 26, 2014, from Oracle Java Documentation: http://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html
Sun Certified Programmer for Java 6 Study Guide. In B. B. Kathy Sierra.

Preparing for OCPJP 7 Certification? Pass in 1st attempt through Whizlabs OCPJP 7 Practice Test !  Start with Free Trial!

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top