2013-12-03 3 views
22

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

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 
    } 
} 
+0

да я не знаю, что я могу просто повторно выдать его ... – membersound

ответ

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

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

try { 
    methodThrowingDifferentExceptions();  
} catch (Exception e) { 
    if(e instanceof SpecificException){ 
     throw e; 
    } 
} 
Смежные вопросы