How to check Exceptions while running
Hi , i wonder how could i check, lets say that there will be
ArrayIndexOutOfBoundsException when trying indexing my array with too big
index value.
Is it possible to check that if there's ArrayIndexOutOfBoundsException , application continues running without showing that exception but diminishing
indexing value as long as its needed to get out of exception situation.
Thanks.
Re: How to check Exceptions while running
A RuntimeException like ArrayIndexOutOfBoundsException generally means your code is flawed and has a bug.
Every array has a length field that you can use to check the index against:
Code:
int i = ...;
int[] arr = ....;
if (i >= arr.length) // index out of bounds
However, without knowing the context of your code, we can't really provide a good solution.
Re: How to check Exceptions while running
Yes, your code is usually flawed if it is possible to get an ArrayIndexOutOfBoundsException. However, if you want to be able to handle this exception, here's how you do it:
Code:
try
{
//Insert Code that throws the exception here
}
catch(ArrayIndexOutOfBoundsException exceptionName)
{
//Insert Code to execute only when the problem occurs
}