Exceptions and exception handling
suggest changeIntroduction
Objects of type Throwable and its subtypes can be sent up the stack with the throw keyword and caught with try…catch statements.
Syntax
void someMethod() throws SomeException { }declares a checked exception. Callers must catch it or declare it themselves.trysurrounds code that might throw.catch (SomeException e)runs when that exception is thrown.finallyruns when control leaves thetry/catch, including byreturnor by a thrown exception. It does not run if the JVM exits first.
try {
someMethod();
} catch (SomeException e) {
System.out.println("SomeException was thrown!");
} finally {
// runs when control leaves try/catch
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents