How to use the try catch finally in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
       try {
            int arr[] = {4, 7, 0, 1, 5};
          
            System.out.println(arr[9]); 
       } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("ArrayIndexOutOfBoundsException");
            System.out.println("Exception: " + e);
       } finally {
            System.out.println("finally");
       } 
    }
}




/*
run:

ArrayIndexOutOfBoundsException
Exception: java.lang.ArrayIndexOutOfBoundsException: Index 9 out of bounds for length 5
finally

*/

 



answered Jul 11, 2022 by avibootz

Related questions

2 answers 305 views
1 answer 224 views
1 answer 241 views
241 views asked Jun 4, 2021 by avibootz
1 answer 243 views
243 views asked Nov 23, 2020 by avibootz
1 answer 431 views
2 answers 302 views
1 answer 172 views
...