Подтвердить что ты не робот

Как поймать все исключения, кроме определенного?

Можно ли перехватить все исключения метода, кроме конкретного, который должен быть выдан?

void myRoutine() throws SpecificException { 
    try {
        methodThrowingDifferentExceptions();
    } catch (SpecificException) {
        //can I throw this to the next level without eating it up in the last catch block?
    } catch (Exception e) {
        //default routine for all other exceptions
    }
}

/Sidenote: помеченный "дубликат" не имеет ничего общего с моим вопросом!

4b9b3361

Ответ 1

void myRoutine() throws SpecificException { 
    try {
        methodThrowingDifferentExceptions();
    } catch (SpecificException se) {
        throw se;
    } catch (Exception e) {
        //default routine for all other exceptions
    }
}

Ответ 2

вы можете сделать это

try {
    methodThrowingDifferentExceptions();    
} catch (Exception e) {
    if(e instanceof SpecificException){
      throw e;
    }
}