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 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.
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.
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.
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.
void myRoutine() throws SpecificException { try { methodThrowingDifferentExceptions(); } catch (SpecificException se) { throw se; } catch (Exception e) { //default routine for all other exceptions } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With