tags:

views:

421

answers:

7

Is it better, less expensive or more readable to use a try/catch block in Java instead of using multiple If statements to check user input for example?

Example when parsing a Date string, won't it be better to parse directly using a try/catch block instead of writing multiple statements looking for illegal characters.

In another example, say i want to read a file or stream, instead of using Scanner, I just force the method and wait for an exception to occur.

Is that a healthy method of programming? Is it less expensive on the Virtual Machine?

UPDATE
Here is an example of what i meant when using DateFormat exception, sometimes it could be a real problem to catch an error, and when doing so, can you guarantee your complicated (usually unreadable) code is error prone?

+21  A: 
Roman
The link in the UPD section is very informative, thanks
medopal
+3  A: 

In general, exceptions are meant for... well, exceptional events. If is meant for things that can occur during the normal course of events.

So typically, if you want to handle a case for which you don't have a good local solution, use an exception to propagate it upwards to someone who can deal with it better. This is significantly more costly than if-else, that's one more reason not to abuse it. A third, IMHO equally important concern is readability: it is much more difficult to follow where the execution flow continues after a throw, than to read a sequence of if statements.

If you want to handle a case locally, most of the time it's better to use a simple if.

Péter Török
+1  A: 

Exceptions are for exceptional cases. It's a bad idea to make program logic dependent on exceptions being thrown.

Amarghosh
A: 

I would think it would be more expensive for the try catch to catch the exception plus the if statements would probably look cleaner.

BrianK
+1  A: 

It s more expensive to use Exception in place of If, because the compiler and the jvm will have more work and difficulty to optimize the control flow of these blocks, and in general exception are for exception.

Patrick
+1  A: 

Exceptions are not good for performance, as they are meant for exceptional circumstances and thus there are tradeoff that make the non-exceptional case faster at the expense of making exceptions slower. That being said, it's impossible to say whether the difference is significant in a particular case without benchmarking that specific case.

But there is another, more important consideration: you should avoid duplication of logic. It's definitely not good to duplicate the date parsing logic to avoid exceptions. Unfortunately, Java does not have the TryParse() methods found in the .NET framework, which allow you to use the framework's parsing logic for sanity testing without exceptions.

In such cases, I would favor using exceptions unless a profiler has identified them as performance bottleneck. But for simpler tests (nulls, end of file), I'd strongly avoid using exceptions as a substitute for if clauses.

Michael Borgwardt
A: 

When an Exception is thrown, the message stack gets copied into the new Exception object in order to enable debugging and printStackTrace(). This is expensive.

If your in-thens get are getting excessive, consider flow-control patterns such as the Strategy, State, and Command patterns.

Matthew Flynn