mercoledì 12 febbraio 2014

Multi-catch exception

Until Java SE 6 the catch block could handle only one type of exception at a time. You therefore had to accumulate several catches to perform a specific action for each type of exception. And as shown in the following code you often have to perform the same action for each exception.
Example: using Several Catch Exception Clauses
try {
// Do something
} catch(SAXExceptione) {
e.printStackTrace();
} catch(IOExceptione) {
e.printStackTrace();
} catch(ParserConfigurationExceptione) {
e.printStackTrace();

}
With Java SE 7 if the handling of each exception is identical, you can add as many exception types as you want, separated by a pipe character as shown in the following code:
Example: Using Multicatch Exception
try {
// Do something
} catch(SAXException | IOException | ParserConfigurationException e) {
e.printStackTrace();

}

Nessun commento:

Posta un commento