Handling ArrayIndexOutOfBoundsException Example
This program demonstrates handling ArrayIndexOutOfBoundsException.
Problem Statement
Write a Java program that handles invalid array index access.
Source Code
| 1 | public class ArrayIndexOutOfBoundsExample { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq ArrayIndexOutOfBounds Example"); |
| 4 | try { |
| 5 | int[] arr={1,2,3}; |
| 6 | System.out.println(arr[5]); |
| 7 | } catch(ArrayIndexOutOfBoundsException e) { |
| 8 | System.out.println("Eduinq Exception: Invalid Array Index"); |
| 9 | } |
| 10 | } |
| 11 | } |
Program Output
Eduinq ArrayIndexOutOfBounds Example Eduinq Exception: Invalid Array Index
Explanation
Accessing invalid array index triggers ArrayIndexOutOfBoundsException. - Try-catch ensures program continues gracefully. - This design improves robustness and prevents crashes. - In enterprise systems, array bounds checking is essential for secure and reliable operations. Conclusion: Shows handling of ArrayIndexOutOfBoundsException.