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:
AssertionError
, orException
.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
.
Initializer |
Throwable(String? description = null, Throwable? cause = null) Parameters:
|
Attributes | |
cause | Source Codeshared Throwable? cause The underlying cause of this exception. |
message | Source 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 |
string | Source Codeshared actual default String string A developer-friendly string representing the instance.
Concatenates the name of the concrete class of the
instance with the Refines Object.string |
suppressed | Source Codeshared Throwable[] suppressed The exceptions that were suppressed in order to propagate this exception. |
Inherited Attributes |
Attributes inherited from: Object |
Methods | |
addSuppressed | Source Codeshared void addSuppressed(Throwable suppressed) The given exception was suppressed in order to propagate this exception. |
printStackTrace | Source 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 |