Starting in JDK 7, exception handling has been expanded with three new features: automatic resource management, multi-catch, and more accurate rethrow.
Multi-catch allows you to catch multiple exceptions with the same catch clause, avoiding code duplication.
To use multi-catch, specify a list of exceptions separated by | in the catch clause. Each parameter is implicitly final.
Usage example: catch(final ArithmeticException | ArrayIndexOutOfBoundsException e) to catch both exceptions with the same catch clause.
The program generates an ArithmeticException when trying to divide by zero and an ArrayIndexOutOfBoundsException when accessing an index outside the bounds of the array. Both exceptions are caught by the same catch clause.
The more precise rethrow feature restricts the type of exception that can be rethrown to:
1 Checked exceptions thrown by the try block.
2 Exceptions not handled by a previous catch clause.
3 Exceptions that are subtype or supertype of the parameter.
The parameter in the catch block must be final to use final recast, which means it cannot be assigned a new value within the catch block. This can be stated explicitly, but is not required.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3