View Single Post
  #1 (permalink)  
Old March 24th, 2013, 01:09 PM
DrGaribaldi DrGaribaldi is offline
Authorized User
Points: 123, Level: 2
Points: 123, Level: 2 Points: 123, Level: 2 Points: 123, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2010
Posts: 18
Thanks: 0
Thanked 6 Times in 3 Posts
Default Using Exceptions for Flow Control?

From a software engineering perspective, using exceptions for flow control is considered a bad practice; exceptions should be used for ... exceptions (exceptional/unexpected behavior, failed preconditions), yet many examples in this book do it. Is this something that Python programmers do often or is the author doing this to illustrate the existence of exceptions? It seems like if the intent is to teach people about a topic, it should be taught in a way that mirrors how it ought to be used (and NOT how it ought NOT be used).

For example, one could argue that there exists a precondition for accessing an element of a dictionary that has_key(key) method should return True. Why not then call that method instead of blindly using the key and catching the exception?