Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch all exceptions except a specific one?

Tags:

java

exception

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!

like image 741
membersound Avatar asked Dec 03 '13 15:12

membersound


People also ask

How do you catch multiple exceptions in a single catch?

Java allows you to catch multiple type exceptions in a single catch block. It was introduced in Java 7 and helps to optimize code. You can use vertical bar (|) to separate multiple exceptions in catch block. An old, prior to Java 7 approach to handle multiple exceptions.

How do you catch specific exceptions?

The order of catch statements is important. Put catch blocks targeted to specific exceptions before a general exception catch block or the compiler might issue an error. The proper catch block is determined by matching the type of the exception to the name of the exception specified in the catch block.

Does except exception catch all exceptions?

Try and Except Statement – Catching all ExceptionsTry and except statements are used to catch and handle exceptions in Python. Statements that can raise exceptions are kept inside the try clause and the statements that handle the exception are written inside except clause.

How do you handle exceptions other than try catch in Java?

if you are working in mvc so you can implementing the OnException(ExceptionContext filterContext) in every controller that you want, this method is called when an unhandled exception occurs. Show activity on this post. Only way is to check for all the conditions that would return an error.


1 Answers

void myRoutine() throws SpecificException {      try {         methodThrowingDifferentExceptions();     } catch (SpecificException se) {         throw se;     } catch (Exception e) {         //default routine for all other exceptions     } } 
like image 137
Dodd10x Avatar answered Sep 24 '22 06:09

Dodd10x