why should exception handling techniques not be used for conventional program control? What is the difference between try block and a catch block?
Lance Finney
Ranch Hand
Joined: Apr 26, 2001
Posts: 133
posted
0
Two reasons: 1) Using exceptions for flow control basically means that you are using exceptions as a goto mechanism, and gotos make for messy, spaghetti code. 2) Creating and catching an exception brings overhead of creating an object. This is much slower and more expensive than using standard flow-control.
The try block is where your normal code codes. The Catch block is where you put code that deals with the problem that caused the exception.
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
posted
0
why should exception handling techniques not be used for conventional program control? This is the third time that this question (more or less) has been asked over the past month. Unfortunately, searching isn't working very well right now. I found one of the past conversations (but it isn't exactly the same topic). The chapter on exception handling of Bruce Eckel's book, Thinking In Java, is worth reading. Also, you may want to try a quick search on google and through the JavaWorld articles for topics such as "exception handling philosophy". What is the difference between try block and a catch block? For a decent introduction to the syntax and structure of exception handling in Java, take a look at The Handling Errors With Exceptions Lesson of Sun's Java Tutorial and chapters 80 and 81 of Bradley Kjell's Introduction to Computer Science using Java. Good Luck.
Performance is also a reason. You run about 18% slower when you use Exception Handling for "normal" control flow ( for example, breaking out of a look when it reaches it's highest value or some "special" value before the end. ). The Performance book from Sun ( Java Platform Performance ) also has an excellent writeup on the performance penalties when "mis-using" Exception Handling: http://www.amazon.com/exec/obidos/ASIN/0201709694/ref=ase_internetconcierg/ I agree on Bruce Eckel "Thinking in Java" as a good recommendations and understanding for Exception Handling. ----------------------------------- Considering the Certified Java Programmer Exam? Get JCertify! http://www.enterprisedeveloper.com/jcertify The Best investment in your career you will make all year
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.