Should we OR exceptions like this ?
catch (final CustomExceptionA | CustomExceptionB e) {
Should we catch expections like this ?
}
It's a fine way to do it if you want to handle them in the exact same way. It'll also only compile on Java 7 (and above).
In Java versions before 7 there was always the issue that if you had to catch multiple exceptions, but (iE) only needed to log them, you had to duplicate a lot of code. Example Java 6:
} catch (NullpointerException e) {
log(e);
} catch (ArrayIndexOutOfBoundsException e) {
log(e);
} catch (NumberFormatException e) {
...
In Java 7 you can use the | operator to simplify this and - the important part - only have to write the error-handling code once, which will avoid common bugs like copy & paste or similar.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.