Please note, this is a STATIC archive of website www.w3schools.com from 05 May 2020, cach3.com does not collect or store any user information, there is no "phishing" involved.
THE WORLD'S LARGEST WEB DEVELOPER SITE

Java catch Keyword

❮ Java Keywords


Example

If an error occur, use try...catch to catch the error and execute some code to handle it:

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

Run example »


Definition and Usage

The catch keyword catches exceptions generated by try statements.

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