Try-Catch for Exception Handling in Java


Use try-catch to handle exceptions and errors in your code gracefully.

Source Code

public class Test {
    public static void main(String[] args) {
        try {
            int[] myNumbers = {1, 2, 3};
            System.out.println(myNumbers[10]); // This will throw an ArrayIndexOutOfBoundsException
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Array index is out of bounds!");
        } finally {
            System.out.println("The 'try catch' is finished.");
        }
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments