This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark """Do not use exceptions for flow control", huh?" Watch """Do not use exceptions for flow control", huh?" New topic
Author

""Do not use exceptions for flow control", huh?

Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Hi Kathy and Bert,

With reference to the Java 2 (Sun certified programmer and developer):

Chapter 4, page 212:

"...Java also throws in a couple of flow control features you might not have used before - exceptions and assertions."

Chapter 12, page 599:

"Do not use exceptions for flow control!"


These two statements appear to be at odds with each other.


Please can you clarify, perhaps giving an example of what NOT to do?


Regards,

Simon


SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
An obvious example of misuse of exceptions:

Perhaps the first sentence would have been better as:
"There are two other Java features that can affect the flow of control: assertions and exceptions. However, they should never be used to expicitly control the flow of execution except in the ways that they are intended to be used. That is, to handle exceptional circumstances arising in your program."


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Exceptions and assertions can be used for flow control, but should not be so used, at least not for flow control in normal program execution.
Both are meant purely for flow control in abnormal program execution, in other words for error handling.


42
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Thanks Barry good example.

Cheers Jeroen.
 
wood burning stoves
 
subject: ""Do not use exceptions for flow control", huh?
 
Similar Threads
Art of Java: Ch 13 Control Flow vs. Exception handling
Refresh the same page
Help with parsing integers from a string
Validating a Number
Question about getting arraylist's size