Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If doSomething throws an exception, whose type is a subclass of Exception, with the message "stack underflow," then the appropriate action will be taken in the exception handler. However, any change to the literals involved will break the code. For example, if a maintainer should edit the throw expression to read throw Exception("Stack Underflow");, the exception will be rethrown by the code of the noncompliant code example rather than handled. Also, an exception may be thrown with no message.

...

It is better to user specific existing exception types or define new, special-purpose exception types.:

Code Block
bgColor#ccccff
public class StackUnderflowException extends Exception {
  StackUnderflowException () {
    super();
  }
  StackUnderflowException (String msg) {
    super(msg);
  }
}

// ...

try {
    doSomething();
} catch(StackUnderflowException sue) {
  // ...
} catch(TimeoutException te) {
  // ...
} catch(SecurityException se) {
  // ...
} catch(Exception e) {
  // ...
  throw e;
}

...

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 likely cause incorrect behavior.

Bibliography