- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to capture out of array index out of bounds exception in Java?
When you try to access an element of an array at an index which is out of range, an ArrayIndexOutOfBoundsException exception is raised.
Example
public class ArrayIndexOutOfBounds { public static void main(String args[]) { try { int[] a = new int[]{1,2,3,4,5}; int x = 6; a[10] = x; } catch(ArrayIndexOutOfBoundsException ex) { System.out.println("Array size is restricted to 5 elements only"); } } }
Output
Array size is restricted to 5 elements only
Advertisements