Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

try

Example

Utilize try…catch to capture errors and execute specific code to manage them if they occur.

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 initiates a try…catch statement.

Within the try block, you can define a section of code to be tested for errors during execution.

In contrast, the catch statement enables you to define a block of code to execute if an error occurs within the try block.