The abstract supertype of values indicating exceptional conditions. An exception may be raised using the throw statement, and handled using the catch clause of the try statement. An instance of Throwable may be passed from throw to catch.

void tryToDoIt() {
    if (canDoIt()) {
        doIt();
    }
    else {
        throw CantDoIt(); //the Throwable
    }
}

try {
    tryToDoIt();
}
catch (CantDoIt e) {
    e.printStackTrace();
}

An instance of Throwable represents a problem, typically an unexpected failure. Either:

  • a unrecoverable error in the program, especially an AssertionError, or
  • a transient, and possibly-recoverable Exception.

The use of the exceptions facility to manage expected failures, that is, failures that are usually handled by the immediate caller of an operation, is discouraged. Instead, the failure should be represented as a return value of the operation being called.

For example, nonexistence of a file should not result in an exception. Instead, an openFile() operation should return the type File?, where a null return value indicates nonexistence. On the other hand, failure to read from an already open file could result in an Exception.

By: Gavin, Tom
Initializer
Throwable(String? description = null, Throwable? cause = null)
Parameters:
  • description = null

    A description of the problem.

  • cause = null

    The underlying cause of this exception.

Attributes
causeSource Codeshared Throwable? cause

The underlying cause of this exception.

messageSource Codeshared default String message

A message describing the problem. This default implementation returns the description, if any, or otherwise the message of the cause, if any.

See also cause
stringSource Codeshared actual default String string

A developer-friendly string representing the instance. Concatenates the name of the concrete class of the instance with the hash of the instance. Subclasses are encouraged to refine this implementation to produce a more meaningful representation.

suppressedSource Codeshared Throwable[] suppressed

The exceptions that were suppressed in order to propagate this exception.

Inherited Attributes
Attributes inherited from: Object
Methods
addSuppressedSource Codeshared void addSuppressed(Throwable suppressed)

The given exception was suppressed in order to propagate this exception.

printStackTraceSource Codeshared void printStackTrace()

Print the stack trace to the standard error of the virtual machine process.

See also printTrace()
Inherited Methods
Methods inherited from: Object