Class TryStmt

All Implemented Interfaces:
NodeWithRange<Node>, NodeWithTokenRange<Node>, Observable, Visitable, HasParentNode<Node>, Cloneable

public class TryStmt extends Statement

The try statement

Java 1.0-6

try {
// ...
} catch (IOException e) {
// ...
} finally {
// ...
}
In this code, "// do things" is the content of the tryBlock, there is one catch clause that catches IOException e, and there is a finally block.

The catch and finally blocks are optional, but they should not be empty at the same time.

Java 7-8

try (InputStream i = new FileInputStream("file")) {
// ...
} catch (IOException|NullPointerException e) {
// ...
} finally {
// ...
}
Java 7 introduced two things:
  • Resources can be specified after "try", but only variable declarations (VariableDeclarationExpr.)
  • A single catch can catch multiple exception types. This uses the IntersectionType.

Java 9+

try (r) {
// ...
} catch (IOException|NullPointerException e) {
// ...
} finally {
// ...
}
Java 9 finishes resources: you can now refer to a resource that was declared somewhere else. The following types are allowed:
  • VariableDeclarationExpr: "X x = new X()" like in Java 7-8.
  • NameExpr: "a".
  • FieldAccessExpr: "x.y.z", "super.test" etc.
See Also: