Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good reasons to avoid using try-catch statements

Example #1:

try { fileChooser.setSelectedFile(new File(filename)); }
catch (NullPointerException e) { /* do nothing */ }

Example #2:

if (filename != null)
    fileChooser.setSelectedFile(new File(filename));

Is #1 inherently bad, for performance or stability (or any other reasons), or is it just a little different? This isn't a very good example because there are no advantages to #1 over #2, but under different circumstances there could be (e.g. improved readability, fewer lines of code, etc.).


Edit: The consensus seems to be that #1 is a no-no. Most popular reason: Overhead

Also, @Raph Levien had a good insight:

One reason to avoid #1 is that it poisons your ability to use exception breakpoints. Normal-functioning code will never purposefully trigger a null pointer exception. Thus, it makes sense to set your debugger to stop every time one happens. In the case of #1, you'll probably get such exceptions routinely. There's also a potential performance impact, among other good reasons.

Also, See Link for more depth.

like image 886
TheBlackKeys Avatar asked Mar 26 '11 03:03

TheBlackKeys


People also ask

Why you should avoid try catch?

Error handling mechanisms like exceptions cause deviations in the usual control flow of the program. These deviations make it harder to reason about the program and are often the source of bugs in the program.

Is it bad practice to use try catch?

It is almost always a bad practice to put try catch in cases of unchecked exception like in the code. And its always a bad practice to catch Exception, the base class of exceptions (unless you are a framework builder, who needs to so that the framework does exception handling.)

Is it better to use if else or try catch?

In general, try-catch blocks are great because they will break (move to the catch statement) whenever the exception occurs. If-else blocks rely on you predicting when the error will happen. Edit: Also, catch blocks won't stop your code from halting when an error is hit.

Should everything be in a try catch?

Putting everything in a try/catch statement is usually a sign of an inexperienced developer who doesn't know much about exceptions IME.


2 Answers

Definitely #2 is a better choice. Exception handling should not be used as a construct in the control flow of your program. Exceptions should be used to handle situations that are not under the control of the programmer. In this example, you can check if the fileChooser is null so that is under your control.

like image 122
Vincent Ramdhanie Avatar answered Nov 11 '22 06:11

Vincent Ramdhanie


Exceptions do bring a certain amount of overhead with them. If they can be avoided, in this case by simply checking for a null, that would be preferred.

As has been said by others, it is also generally bad practice to use Exceptions for flow control. For a detailed explanation of why this is bad, check out this.

Here is a short blurb from that answer:

Exceptions are basically non-local goto statements with all the consequences of the latter. Using exceptions for flow control violates a principle of least astonishment, make programs hard to read (remember that programs are written for programmers first).

like image 43
Corey Sunwold Avatar answered Nov 11 '22 07:11

Corey Sunwold