bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java try Keyword

❮ Java Keywords

Example

try {
  int[] myNumbers = {1, 2, 3};
  System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
}

Definition and Usage

The try keyword creates a try...catch statement.

The try statement allows you to define a block of code to be tested for errors while it is being executed.

The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

Related Pages

Read more about exceptions in our Java Try..Catch Tutorial .

❮ Java Keywords

Previous

Java transient Keyword

Next

Java var Keyword