Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use exceptions in Java (example)

I know that this would be bad practice although I know that I would not be able to explain why.

int [] intArr = ...
...
try{
   int i = 0;
   while(true){
      System.out.println(intArr[i++]);
   }
}catch(ArrayIndexOutOfBoundsException e){}

I think that you are only supposed to use exceptions for things that shouldn't happen. I am asking this question because I think that I am using exceptions wrong sometimes. If your programming is running a standard case should exceptions be thrown?

This seems related: Preventing exceptions vs. catching exceptions in Java

like image 221
sixtyfootersdude Avatar asked Jul 09 '10 13:07

sixtyfootersdude


4 Answers

It's wrong because you know that eventually the loop will reach the last element of intArr, so there's nothing exceptional in this, you're actually expecting this behaviour.

like image 137
Alberto Zaccagni Avatar answered Oct 06 '22 15:10

Alberto Zaccagni


You are right: exceptions are meant for, ehm, exceptional cases. Using them for controlling normal control flow is not only obscuring the intent of the code (which would be enough to disqualify it already), but also is much slower, since throwing and catching exceptions is costly.

The standard idiom (in Java5 and above) is using a foreach loop:

for (int i : intArr) {
  System.out.println(i);
}
like image 22
Péter Török Avatar answered Oct 06 '22 14:10

Péter Török


Catching exceptions is only a bad practice when it concerns a RuntimeException. The ArrayIndexOutOfBoundsException which you're trying to catch there is one.

RuntimeExceptions identify programmatically recoverable problems which are caused by faults in code flow. You should not fix them by catching them, but by writing proper code and making use of flow control statements like if/else, while, for, etc.

See also:

  • When to throw which exception?
  • Exceptions tutorial
like image 5
BalusC Avatar answered Oct 06 '22 15:10

BalusC


As always "it depends" and you will find many differing opinions. Here's mine

  • Exceptions fall into two general categories.
    • Things you can reasonably anticipate and handle (FileNotFoundException)
    • Things you generally don't antipicate assuming perfect code (ArrayIndexOutOfBounds)

You would expect to generally handle the first category and not the latter. The latter is usually programming errors.

Your example falls into the latter case, a programming error. The exception is intended to give good information about the failure at runtime, not act as a control flow.

Some people will say that the first is checked exceptions and the second is unchecked. I would disagree with that. I almost always find checked exceptions a pain in reality as you almost always end up doing catch/wrap/rethrow to another exception type. When throwing exceptions and defining my own exception classes I almost always use unchecked.

like image 3
Mike Q Avatar answered Oct 06 '22 15:10

Mike Q