...
Code Block | ||
---|---|---|
| ||
try { doSomething(); } catch (Throwable e) { String msg = e.getMessage(); switch (msg) { case "stack underflow": // handleHandle error break; case "connection timeout": // handleHandle error break; case "security violation": // handleHandle error break; default throw e; } } |
...
Code Block | ||
---|---|---|
| ||
public class StackUnderflowException extends Exception { StackUnderflowException () { super(); } StackUnderflowException (String msg) { super(msg); } } // ... try { doSomething(); } catch (StackUnderflowException sue) { // handleHandle error } catch (TimeoutException te) { // handleHandle error } catch (SecurityException se) { // handleHandle error } |
Applicability
Exceptions are used to handle exceptional conditions. If an exception is not caught, the program will be terminated. An exception that is incorrectly caught or is caught at the wrong level of recovery will often cause incorrect behavior.
...