Breaking News
Home / Java / Java FAQ / How does Try/Catch/Finally work in Java?

How does Try/Catch/Finally work in Java?

Answer

try {

// Normally this code runs from the top of the block to the bottom
// without problems. But it can sometimes throw and exception,
// either directly or with a throw statement or intdirectly by calling
// a method that throws and exception.
}
catch (SomeException e1) {
// This block contains statements that handle the exception object
// of type SomeException or a subclass of that type. Statements in
// this block can refer to that exception object by the name e1.
}
catch (AnotherException e2) {
// This block contains statements that handle the exception object
// of type AnotherException or a subclass of that type. Statements in
// this block can refer to that exception object by the name e2.
}
finally {
// This block contains statements that are ALWAYS executed
// after leaving the try clause, regardless of whether we leave it:
// 1) normally after reaching the bottom of the block;
// 2) because of a break, continue, or return statement;
// 3) with an exception handled by a catch clause above; or
// 4) with an uncaught exception that has not been handled.
// If the try clause calls System.exit(), however, the interpreter
// exits before the finally clause can be run.
}

[pb_builder]

Check Also

When is OCAJP Java certification going live?

After completing the BETA phase of testing last year, the Oracle’s new OCAJP Java certification, …

Advertisment ad adsense adlogger