catch clause which removes redundant code.
Here is an example of a statement which throws multiple exceptions:
try {
Class.forName("Object").newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
In JDK7, you can collapse the catch blocks into a single one:
try {
Class.forName("Object").newInstance();
} catch (ClassNotFoundException |
InstantiationException |
IllegalAccessException e) {
e.printStackTrace();
}
Eclipse Support:- If you have multiple catch clauses, quick fix gives you the option to Combine catch blocks provided all the catch bodies are the same
- Conversely, if you have a multi-catch clause, quick fix gives you the option to Use separate catch blocks so that you can handle each exception separately
- Quick fix gives you the option to Move exception to separate catch block which allows you to take exceptions out of the multi-catch in case you want to handle them separately
- Quick fix also gives you the option to Surround with try/multi-catch
Handling More Than One Type of Exception
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.