How to catch all exceptions except a specific one?

JavaException

Java Problem Overview


Is it possible to catch all exceptions of a method, except for a specific one, which should be thrown?

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: the marked "duplicate" has nothing to do with my question!

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

you can do like this

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmembersoundView Question on Stackoverflow
Solution 1 - JavaDodd10xView Answer on Stackoverflow
Solution 2 - JavaPrabhakaran RamaswamyView Answer on Stackoverflow