‘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:
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:
- ‘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) - ‘finally’ clauses are not required.
Next, we see how specific exceptions and general exceptions must be stated.
Now consider the following piece of code:
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.
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!
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021