Top Description Fields Constructors Methods
com.github.javaparser.ast.stmt

public Class TryStmt

extends Statement
Class Inheritance
Imports
com.github.javaparser.TokenRange, com.github.javaparser.ast.AllFieldsConstructor, .Node, .NodeList, .Generated, com.github.javaparser.ast.expr.Expression, com.github.javaparser.ast.observer.ObservableProperty, com.github.javaparser.ast.visitor.CloneVisitor, .GenericVisitor, .VoidVisitor, com.github.javaparser.metamodel.JavaParserMetaModel, .OptionalProperty, .TryStmtMetaModel, java.util.Optional, java.util.function.Consumer

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:

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:
Author
Julio Vilmar Gesser
See Also
CatchClause, com.github.javaparser.ast.type.IntersectionType, com.github.javaparser.ast.expr.FieldAccessExpr, com.github.javaparser.ast.expr.NameExpr

Field Summary

Modifier and TypeField and Description
private NodeList<CatchClause>
private BlockStmt
private NodeList<Expression>
private BlockStmt

Constructor Summary

AccessConstructor and Description
public
public
TryStmt(final BlockStmt tryBlock, final NodeList<CatchClause> catchClauses, final BlockStmt finallyBlock)

public
TryStmt(NodeList<Expression> resources, final BlockStmt tryBlock, final NodeList<CatchClause> catchClauses, final BlockStmt finallyBlock)

public
TryStmt(TokenRange tokenRange, NodeList<Expression> resources, BlockStmt tryBlock, NodeList<CatchClause> catchClauses, BlockStmt finallyBlock)

This constructor is used by the parser and is considered private.

Method Summary

Modifier and TypeMethod and Description
public <R, A> R
accept(final GenericVisitor<R, A>
the visitor implementation
v
,
final A
the argument passed to the visitor (of type A)
arg
)

Implements com.github.javaparser.ast.visitor.Visitable.accept.

Accept method for visitor support.
public <A> void
accept(final VoidVisitor<A>
the visitor implementation
v
,
final A
any value relevant for the visitor (of type A)
arg
)

Implements com.github.javaparser.ast.visitor.Visitable.accept.

Accept method for visitor support.
public TryStmt
public TryStmt
clone()

Overrides com.github.javaparser.ast.stmt.Statement.clone.

Creates and returns a copy of this object.
public NodeList<CatchClause>
public Optional<BlockStmt>
public TryStmtMetaModel
public NodeList<Expression>
public BlockStmt
public void
public boolean
public boolean
public TryStmt
public TryStmt
public boolean
public TryStmt
setCatchClauses(final NodeList<CatchClause> catchClauses)

public TryStmt
setFinallyBlock(final BlockStmt finallyBlock)

public TryStmt
setResources(final NodeList<Expression> resources)

public TryStmt
setTryBlock(final BlockStmt tryBlock)

public Optional<TryStmt>
Inherited from com.github.javaparser.ast.stmt.Statement:
asAssertStmtasBlockStmtasBreakStmtasContinueStmtasDoStmtasEmptyStmtasExplicitConstructorInvocationStmtasExpressionStmtasForEachStmtasForStmtasIfStmtasLabeledStmtasLocalClassDeclarationStmtasReturnStmtasSwitchStmtasSynchronizedStmtasThrowStmtasUnparsableStmtasWhileStmtasYieldStmtifAssertStmtifBlockStmtifBreakStmtifContinueStmtifDoStmtifEmptyStmtifExplicitConstructorInvocationStmtifExpressionStmtifForEachStmtifForStmtifIfStmtifLabeledStmtifLocalClassDeclarationStmtifReturnStmtifSwitchStmtifSynchronizedStmtifThrowStmtifUnparsableStmtifWhileStmtifYieldStmtisAssertStmtisBlockStmtisBreakStmtisContinueStmtisDoStmtisEmptyStmtisExplicitConstructorInvocationStmtisExpressionStmtisForEachStmtisForStmtisIfStmtisLabeledStmtisLocalClassDeclarationStmtisReturnStmtisSwitchStmtisSynchronizedStmtisThrowStmtisUnparsableStmtisWhileStmtisYieldStmttoAssertStmttoBlockStmttoBreakStmttoContinueStmttoDoStmttoEmptyStmttoExplicitConstructorInvocationStmttoExpressionStmttoForEachStmttoForStmttoIfStmttoLabeledStmttoLocalClassDeclarationStmttoReturnStmttoSwitchStmttoSynchronizedStmttoThrowStmttoUnparsableStmttoWhileStmttoYieldStmt

Field Detail

catchClausesback to summary
private NodeList<CatchClause> catchClauses
finallyBlockback to summary
private BlockStmt finallyBlock
Annotations
@OptionalProperty
resourcesback to summary
private NodeList<Expression> resources
tryBlockback to summary
private BlockStmt tryBlock

Constructor Detail

TryStmtback to summary
public TryStmt()
TryStmtback to summary
public TryStmt(final BlockStmt tryBlock, final NodeList<CatchClause> catchClauses, final BlockStmt finallyBlock)
TryStmtback to summary
public TryStmt(NodeList<Expression> resources, final BlockStmt tryBlock, final NodeList<CatchClause> catchClauses, final BlockStmt finallyBlock)
Annotations
@AllFieldsConstructor
TryStmtback to summary
public TryStmt(TokenRange tokenRange, NodeList<Expression> resources, BlockStmt tryBlock, NodeList<CatchClause> catchClauses, BlockStmt finallyBlock)

This constructor is used by the parser and is considered private.

Annotations
@Generated:com.github.javaparser.generator.core.node.MainConstructorGenerator

Method Detail

acceptback to summary
public <R, A> R accept(final GenericVisitor<R, A> v, final A arg)

Implements com.github.javaparser.ast.visitor.Visitable.accept.

Doc from com.github.javaparser.ast.visitor.Visitable.accept.

Accept method for visitor support.

Parameters
<R>
the type of the return value of the visitor
<A>
the type the user argument passed to the visitor
v:GenericVisitor<R, A>

the visitor implementation

arg:A

the argument passed to the visitor (of type A)

Returns:R

the result of the visit (of type R)

Annotations
@Override
@Generated:com.github.javaparser.generator.core.node.AcceptGenerator
acceptback to summary
public <A> void accept(final VoidVisitor<A> v, final A arg)

Implements com.github.javaparser.ast.visitor.Visitable.accept.

Doc from com.github.javaparser.ast.visitor.Visitable.accept.

Accept method for visitor support.

Parameters
<A>
the type the argument passed for the visitor
v:VoidVisitor<A>

the visitor implementation

arg:A

any value relevant for the visitor (of type A)

Annotations
@Override
@Generated:com.github.javaparser.generator.core.node.AcceptGenerator
asTryStmtback to summary
public TryStmt asTryStmt()

Overrides com.github.javaparser.ast.stmt.Statement.asTryStmt.

Annotations
@Override
@Generated:com.github.javaparser.generator.core.node.TypeCastingGenerator
cloneback to summary
public TryStmt clone()

Overrides com.github.javaparser.ast.stmt.Statement.clone.

Doc from java.lang.Object.clone.

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:

x.clone() != x
will be true, and that the expression:
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
x.clone().equals(x)
will be true, this is not an absolute requirement.

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

Returns:TryStmt

a clone of this instance.

Annotations
@Override
@Generated:com.github.javaparser.generator.core.node.CloneGenerator
getCatchClausesback to summary
public NodeList<CatchClause> getCatchClauses()
Annotations
@Generated:com.github.javaparser.generator.core.node.PropertyGenerator
getFinallyBlockback to summary
public Optional<BlockStmt> getFinallyBlock()
Annotations
@Generated:com.github.javaparser.generator.core.node.PropertyGenerator
getMetaModelback to summary
public TryStmtMetaModel getMetaModel()

Overrides com.github.javaparser.ast.stmt.Statement.getMetaModel.

Returns:TryStmtMetaModel

Doc from com.github.javaparser.ast.Node.getMetaModel.

get JavaParser specific node introspection information.

Annotations
@Override
@Generated:com.github.javaparser.generator.core.node.GetMetaModelGenerator
getResourcesback to summary
public NodeList<Expression> getResources()
Annotations
@Generated:com.github.javaparser.generator.core.node.PropertyGenerator
getTryBlockback to summary
public BlockStmt getTryBlock()
Annotations
@Generated:com.github.javaparser.generator.core.node.PropertyGenerator
ifTryStmtback to summary
public void ifTryStmt(Consumer<TryStmt> action)

Overrides com.github.javaparser.ast.stmt.Statement.ifTryStmt.

Annotations
@Generated:com.github.javaparser.generator.core.node.TypeCastingGenerator
isTryStmtback to summary
public boolean isTryStmt()

Overrides com.github.javaparser.ast.stmt.Statement.isTryStmt.

Annotations
@Override
@Generated:com.github.javaparser.generator.core.node.TypeCastingGenerator
removeback to summary
public boolean remove(Node node)

Overrides com.github.javaparser.ast.stmt.Statement.remove.

Annotations
@Override
@Generated:com.github.javaparser.generator.core.node.RemoveMethodGenerator
removeFinallyBlockback to summary
public TryStmt removeFinallyBlock()
Annotations
@Generated:com.github.javaparser.generator.core.node.RemoveMethodGenerator
removeTryBlockback to summary
public TryStmt removeTryBlock()
Annotations
@Generated:com.github.javaparser.generator.core.node.RemoveMethodGenerator
replaceback to summary
public boolean replace(Node node, Node replacementNode)

Overrides com.github.javaparser.ast.stmt.Statement.replace.

Annotations
@Override
@Generated:com.github.javaparser.generator.core.node.ReplaceMethodGenerator
setCatchClausesback to summary
public TryStmt setCatchClauses(final NodeList<CatchClause> catchClauses)
Annotations
@Generated:com.github.javaparser.generator.core.node.PropertyGenerator
setFinallyBlockback to summary
public TryStmt setFinallyBlock(final BlockStmt finallyBlock)
Annotations
@Generated:com.github.javaparser.generator.core.node.PropertyGenerator
setResourcesback to summary
public TryStmt setResources(final NodeList<Expression> resources)
Annotations
@Generated:com.github.javaparser.generator.core.node.PropertyGenerator
setTryBlockback to summary
public TryStmt setTryBlock(final BlockStmt tryBlock)
Annotations
@Generated:com.github.javaparser.generator.core.node.PropertyGenerator
toTryStmtback to summary
public Optional<TryStmt> toTryStmt()

Overrides com.github.javaparser.ast.stmt.Statement.toTryStmt.

Annotations
@Override
@Generated:com.github.javaparser.generator.core.node.TypeCastingGenerator