I don’t see an array anywhere in your code, so that’s maybe why the try block isn’t catching anything (I assume there is an array in one of the called methods?). Also, you really, really shouldn’t allow your program to read outside the bounds of an array. That’s just bad design. That being said, here is how you would catch the exception in the clearest way I can think of:
try { array[index] = someValue; } catch(ArrayIndexOutOfBoundsException exception) { handleTheExceptionSomehow(exception); }
Or do as @Peerhenry suggests and just throw a new Exception if the indices aren’t correct, which would be a much better design.
Related Posts:
- must declare a named package eclipse because this compilation unit is associated to the named module
- Invalid escape sequence (valid ones are \b \t \n \f \r \” \’ \\ )
- Eclipse IDE – Error: Build path specifies execution environment Java SE 1.7
- Eclipse IDE – Error: Build path specifies execution environment Java SE 1.7
- What is a StackOverflowError?
- How to uninstall Eclipse?
- How do I determine whether an array contains a particular value in Java?
- How can I solve Exception in thread “main” java.lang.NullPointerException error [duplicate]
- Why am I getting a NoClassDefFoundError in Java?
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- Using Pairs or 2-tuples in Java [duplicate]
- Is there a way to convert all comments into javadoc comments? [Eclipse/Java]
- Reverse a string in Java
- “Char cannot be dereferenced” error
- Explanation on Integer.MAX_VALUE and Integer.MIN_VALUE to find min and max value in an array
- Error occurred during initialization of boot layer FindException: Module not found
- Non-static variable cannot be referenced from a static context
- SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”
- “Missing return statement” within if / for / while
- How to convert byte array to string and vice versa?
- good example of Javadoc
- Why do JVM arguments start with “-D”?
- How to split a string in Java
- Printing out a linked list using toString
- Received fatal alert: handshake_failure through SSLHandshakeException
- Best way to Format a Double value to 2 Decimal places
- Collision resolution in Java HashMap
- “Content is not allowed in prolog” when parsing perfectly valid XML on GAE
- Java Undefined Object
- How to identify object types in java
- What is the result of making log4j additivity equals to true or false?
- Attempt to invoke virtual method ‘…’ on a null object reference
- Is there a Mutex in Java?
- maven error: package org.junit does not exist
- ClassNotFoundException: org.slf4j.LoggerFactory
- Problems with setting the classpath in ant
- Spring Data JPA Update @Query not updating?
- What does ‘SSSXXX’ mean in a java simple date format object?
- Server Tomcat v9.0 Server at localhost failed to start
- Getting java.net.SocketTimeoutException: Connection timed out in android
- What is Java String interning?
- Cannot change version of project facet Dynamic Web Module to 3.0?
- InvalidKeyException Illegal key size
- Collections.emptyMap() vs new HashMap()
- java howto ArrayList push, pop, shift, and unshift
- Error Message: Can’t determine a valid Java Home
- How can I fix ‘android.os.NetworkOnMainThreadException’?
- Eclipse does not start when I run the exe?
- How to print to the console in Android Studio?
- When is the finalize() method called in Java?
- Java “user.dir” property – what exactly does it mean?
- it throws me error incompatible types: unexpected return value — return 10; please correct it make note where i am doing wrong code?
- Caused by: java.security.UnrecoverableKeyException: Cannot recover key
- How can I truncate a double to only two decimal places in Java?
- How do I add a simple jQuery script to WordPress?
- I get the error int cannot be converted to boolean?c
- Can’t install JDK 9 because “Another Java installation is in progress”
- java.sql.SQLException: No database selected – why?
- Java: How to access methods from another class
- Cannot make a static reference to the non-static method fxn(int) from the type Two
- What is causing this Java “Cannot find symbol” error?
- What does equals(Object obj) do?
- How do I make the method return type generic?
- keytool error bash: keytool: command not found
- Java Error: The constructor is undefined
- Java Embedding Into HTML
- Any way to declare an array in-line?
- java.io.StreamCorruptedException: invalid stream header: 54657374
- How to cast ArrayList<> from List<>
- Best way to “negate” an instanceof
- How to send HTTP request in java?
- How to take input as String with spaces in java using scanner
- Address already in use: JVM_Bind java
- java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [duplicate]
- How can I check if a value is of type Integer?
- Java 3D Game Engine?
- How to cast ArrayList<> from List<>
- Using insertion sort for descending order?
- What is the home directory for JDK?
- java.lang.ClassNotFoundException: org.postgresql.Driver, Android
- Java – Change int to ascii
- Adding JPanel to JFrame
- Append a single character to a string or char array in java?
- Java printf formatting to print items in a table or columns
- java.lang.IllegalStateException: Failed to introspect Class
- ShoppingCart.Java Program Assignment
- White spaces are required between publicId and systemId
- Http status 401 This request requires HTTP authentication (). in tomcat 6
- Get integer value of the current year in Java
- How to make the division of 2 ints produce a float instead of another int?
- Java Console Prompt for ENTER input before moving on [duplicate]
- Can’t load IA 32-bit .dll on a AMD 64-bit platform
- Android Studio 2.2.3: Gradle project sync failed. Basic functionality (e.g. editting, debugging) will not work properly
- How to draw a circle with given X and Y coordinates as the middle spot of the circle?
- In Java, how do I parse XML as a String instead of a file?
- Java Returning method which returns arraylist?
- String cannot be resolved to a type – Java RAD
- IntelliJ IDEA “The selected directory is not a valid home for JDK”
- Simple timeout in java
- Eclipse message saying List cannot be resolved to a type