Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thorough use of 'if' statements or 'try/catch' blocks?

Give me some of your thoughts on which is a better coding practice/makes more efficient code/looks prettier/whatever: Increasing and improving your ability to use if statements to anticipate and catch potential problems? Or simply making good use of try/catch in general?

Let's say this is for Java (if it matters).

Edit: I'm presently transitioning myself away from some admittedly out-dated and constrained current coding practices, but I'm a little torn on the necessity of doing so on a few points (such as this). I'm simply asking for some perspectives on this. Not a debate.

like image 858
Daddy Warbox Avatar asked Nov 30 '08 15:11

Daddy Warbox


2 Answers

this debate (2003) was good:

http://www.joelonsoftware.com/items/2003/10/13.html

http://nedbatchelder.com/blog/200310/joel_on_exceptions.html

like image 75
Gene T Avatar answered Sep 28 '22 07:09

Gene T


if blocks are slightly faster; if you aren't going to need a dozen of them, they're a better idea than try/catches. Exceptions should be exceptional, not every time the code runs. I use Exceptions for rare events like server disconnections (even though they happen a few times every day), and if blocks for any of my controllable variables.

like image 45
Karl Avatar answered Sep 28 '22 07:09

Karl