Come determinare il tipo di eccezione rilevata, se un'operazione rileva più eccezioni?Java - Come verificare quale tipo di eccezione è stato generato?
Questo esempio dovrebbe rendere più senso:
try {
int x = doSomething();
} catch (NotAnInt | ParseError e) {
if (/* thrown error is NotAnInt */) { // line 5
// printSomething
} else {
// print something else
}
}
On line 5, come posso controllare quale eccezione è stato catturato?
Ho provato if (e.equals(NotAnInt.class)) {..}
ma senza fortuna.
NOTA: NotAnInt
e ParseError
sono classi nel mio progetto che si estendono Exception
.
fanno recidere le catture: cattura (NotAnInt) {} catch (parseError) { } –
Possibile duplicato di [Java - Throwable to Exception] (https://stackoverflow.com/questions/12359175/java-throwable-to-exception) – Molham