Wie kann ich feststellen, welche Art von Ausnahme abgefangen wurde, wenn eine Operation mehrere Ausnahmen erfasst?Java - Wie überprüft man, welcher Exception-Typ ausgelöst wurde?
sollte dieses Beispiel mehr Sinn machen:
try {
int x = doSomething();
} catch (NotAnInt | ParseError e) {
if (/* thrown error is NotAnInt */) { // line 5
// printSomething
} else {
// print something else
}
}
Zeile 5, wie kann ich überprüfen, welche Ausnahme gefangen wurde?
Ich versuchte if (e.equals(NotAnInt.class)) {..}
aber kein Glück.
HINWEIS: NotAnInt
und ParseError
sind Klassen in meinem Projekt, die Exception
erweitern.
Fänge trennen: catch (NotAnInt) {} catch (ParseError) { } –
Mögliches Duplikat von [Java - Throwable to Exception] (https://stackoverflow.com/questions/12359175/java-throwable-to-exception) – Molham