error handling

Farshid Lashkari no at spam.com
Thu Aug 10 22:50:41 EDT 2006


Steven D'Aprano wrote:
> That's broken. 
> 
> Imagine that somewhere in main() the following is called:
> 
> D = {"a": "apple", "b": "bicycle", "c": "cat"}
> print D["aardvark"]
> 
> Your code now prints "That number is way too big!". That's not good.
> 
> try...except blocks should, as a general rule, cover only the smallest
> amount of code that they need to.

Hi Steven,

Point taken and I think your solution better addresses the root of the 
problem. However, the OP said he wanted to print out that error message 
whenever the interpreter came across any IndexError. So I gave him what 
he wanted. I guess he needs to be more careful what he wishes for ;)

-Farshid



More information about the Python-list mailing list