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 298 views
1 answer 217 views
1 answer 233 views
233 views asked Jun 4, 2021 by avibootz
1 answer 235 views
235 views asked Nov 23, 2020 by avibootz
1 answer 416 views
2 answers 297 views
1 answer 170 views
...