Home > DeveloperSection > Articles > Exception Handling in Java: Classifying Exceptions

Exception Handling in Java: Classifying Exceptions


Java Java 
Ratings:
0 Comment(s)
 144  View(s)
Rate this:

Exception Handling in Java: Classifying Exceptions 


At the top of the Exception class hierarchy we have the Throwable class. All other exception classes, including our own designed exception classes, inherit from the Throwable class. Both Error and Exception are subclasses of Throwable. As discussed earlier:

·         The Error class denotes the fatal errors

·         The Exception class denotes the non-fatal errors

We will be focusing on the Exception class hierarchy, which has several subclasses, each meeting a specific situation. For example, the class ArrayIndexOutOfBoundsException is used for designating an illegal access to an array element, whereas the class ArithmeticException describes an exception that may occur during an arithmetic operation.

The purpose behind creating this exception hierarchy is to give us the option of treating various exception cases differently and to allow specialized information to reside in exception classes specific to particular situations.



Several subclasses of the Exception class are provided for this purpose so that we can catch a specific type of exception. For each specific type of exception we want to capture, we have to write a separate catch block. Thus, our code will consist of multiple catch blocks when we want to handle different types of exceptions differently. The order in which these blocks are defined in our code is also important. When the code inside a try block throws an exception, its catch clauses are examined in their order of appearance in the source file. Our program should first try to catch an exception of a subclass type. If no such subclass exceptions are handled, eventually your code provides an exception handler for the most generic Exception class. The syntax for incorporating this feature is shown here:

try {

     blockStatements

} catch ( ExceptionType1 exceptionObjectName1 ) {

      blockStatements

}

...

} catch ( ExceptionTypeN exceptionObjectNameN ) {

      blockStatements

}

 

A typical try with multiple catch blocks looks like this:

try {

// code that may generate a runtime exception

} catch (ReadOnlyBufferException e1) {

// your error handler

} catch (UnsupportedOperationException e2) {

// your error handler

} catch (Exception e) { // other catch blocks

// your error handler

}

 

 

Java SE 7 has added new syntax for catching multiple exception types in the same catch clause, as shown here:

try {

// code that may generate a runtime exception

} catch (ReadOnlyBufferException | UnsupportedOperationException e1) {

// your error handler

}

 

 

When we use this syntax, we provide a common error handler for all these types of exceptions.

Also, a subclass exception must be handled before its superclass exception. If we write an Exception handler block as the first block in our multiple-exception-handler code, this would always get called whenever an exception occurs and the code provided in other exception handlers would never get called. The compiler catches this error and complains about “unreachable code.”


Don't want to miss updates? Please click the below button!

Follow MindStick