How to Use Try-catch Statement in Java


In this example, we will learn you how to use the try…catch statement in Java.

Source Code

package com.beginner.examples;

public class TryCatch {
  public static void main(String[] args) {
    // use the try...catch statement
    try {
      int[] nums = {3, 7, 15};
      System.out.println(nums[9]);
    } catch (Exception e) {
      System.out.println("Error");
    }
  }
}

Output:

Error
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments