4

not that familiar with JAVA or exception handling. Looking for some advice on what is acceptable and what is frowned upon.

The scenario, i'm building a game of life program, I have conditionals set up to check if a cell will be out of bounds and not try to access that 'cell'. My question is, is it acceptable to use a try catch block instead of 8 conditionals, and just do nothing if the arrayOutOfBounds exception is thrown. ie ignore the cells out of bounds, or is this bad practice? for instance...

try{
    neighbors += cellIsAlive(row, col);
}catch(ArrayIndexOutofBoundsException e)
{
    //dont do anything and continue counting neighbors
}

In this scenario cellIsAlive method checks a location in a multi dimensional array and returns 1 if it's alive 0 otherwise and throws ArrayIndexOutofBoundsException.

Is this a good idea or is it bad practice to use exceptions this way?

Thanks ahead of time for any input.

7
  • 1
    haha sorry, my php coming out. will edit. Commented Feb 20, 2014 at 8:17
  • Exceptions were made to handle... exceptions. But since you can know the problem, it's better for you to simply check the indexes.. Commented Feb 20, 2014 at 8:18
  • you should never swallow an exception, you simply don't! Commented Feb 20, 2014 at 8:19
  • @ Maroun I was thinking that, but it seems like this would greatly simplify the code and make it more readable,like I said I'm not that experienced in this situation. Seems like everyone agrees the conditionals are better though. Commented Feb 20, 2014 at 8:19
  • 1
    In general, using exceptions to control program flow is frowned upon because it's normally much cheaper to check bounds than it is to create and then catch the exception (though I believe that cost has come down as the JVM has evolved). Commented Feb 20, 2014 at 8:20

3 Answers 3

6

It's absolutely a bad practice. Exception handling consumes a lot of resources and should be used only (as its name implies) for exceptional cases.

Take a look at chapter 9 of this book (and also read the rest when you can):

http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683/

You'll see that what you're trying to do is very similar to the example used for illustrating what you're not supposed to do, and I quote:

Someday, if you are unlucky, you may stumble across a piece of code that looks something like this:

// Horrible abuse of exceptions. Don't ever do this!
try {
    int i = 0;
    while(true)
        range[i++].climb();
} catch(ArrayIndexOutOfBoundsException e) {
}

What does this code do? It’s not at all obvious from inspection, and that’s reason enough not to use it (Item 55). It turns out to be a horribly ill-conceived idiom for looping through the elements of an array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, both for the information and the book reccomendation.
3

It's a bad practice to catch RuntimeExcepions. ArrayIndexOutofBoundsException is a subclass of RuntimeException. RuntimeExceptions are programmers fault, You should catch only checked Exceptions.

Comments

0

Catching ArrayIndexOutofBoundsException wont help you andcatching runtime exceptions is a bad habbit . Fix the logic inside cellIsActive() method ..

Use http://www.tutorialspoint.com/java/java_exceptions.htm for more details

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.