Home | | Web Programming | Three Recently Added Exception Features

Chapter: Java The Complete Reference : The Java Language : Exception Handling

Three Recently Added Exception Features

Beginning with JDK 7, three interesting and useful features have been added to the exception system.

Three Recently Added Exception Features

 

Beginning with JDK 7, three interesting and useful features have been added to the exception system. The first automates the process of releasing a resource, such as a file, when it is no longer needed. It is based on an expanded form of the try statement called try-with-resources, and is described in Chapter 13 when files are introduced. The second feature is called multi-catch, and the third is sometimes referred to as final rethrow or more precise rethrow. These two features are described here.

 

The multi-catch feature allows two or more exceptions to be caught by the same catch clause. It is not uncommon for two or more exception handlers to use the same code sequence even though they respond to different exceptions. Instead of having to catch each exception type individually, you can use a single catch clause to handle all of the exceptions without code duplication.

To use a multi-catch, separate each exception type in the catch clause with the OR operator. Each multi-catch parameter is implicitly final. (You can explicitly specify final, if desired, but it is not necessary.) Because each multi-catch parameter is implicitly final, it can’t be assigned a new value.

Here is a catch statement that uses the multi-catch feature to catch both

 

ArithmeticException and ArrayIndexOutOfBoundsException:

 

catch(ArithmeticException | ArrayIndexOutOfBoundsException e) {

 

The following program shows the multi-catch feature in action:

 

// Demonstrate the multi-catch feature. 

class MultiCatch {

 

public static void main(String args[]) { int a=10, b=0;

 

int vals[] = { 1, 2, 3 };

 

try {

 

int result = a / b; // generate an ArithmeticException

 

     vals[10] = 19; // generate an ArrayIndexOutOfBoundsException

 

// This catch clause catches both exceptions.

 

} catch(ArithmeticException | ArrayIndexOutOfBoundsException e) { System.out.println("Exception caught: " + e);

 

}

 

System.out.println("After multi-catch.");

 

}

 

}

 

The program will generate an ArithmeticException when the division by zero is attempted. If you comment out the division statement and remove the comment symbol from the next line, an ArrayIndexOutOfBoundsException is generated. Both exceptions are caught by the single catch statement.

 

The more precise rethrow feature restricts the type of exceptions that can be rethrown to only those checked exceptions that the associated try block throws, that are not handled by a preceding catch clause, and that are a subtype or supertype of the parameter. Although this capability might not be needed often, it is now available for use. For the more precise rethrow feature to be in force, the catch parameter must be either effectively final, which means that it must not be assigned a new value inside the catch block, or explicitly declared final.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Language : Exception Handling : Three Recently Added Exception Features |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.