[Q] Are Exceptions used that much in practice?

Roy Katz katz at Glue.umd.edu
Tue Dec 12 22:31:47 EST 2000


On Wed, 13 Dec 2000, Rainer Deyke wrote:


> [snip]
> When the length of the sequence is 0, this throws a ZeroDivisionError.  You
> can handle this with a try clause:
> 
> def average(l):
>   total = 0
>   for item in l:
>     total += item
>   try:
>     total /= len(l)
>   except ZeroDivisionError:
>     return 0
>
>  [snip] 
> 
> This last option is actually the best in most cases.  If the caller wants to
> pass an empty sequence, the caller still has the option of catching the
> ZeroDivisionError:
> 
> try:
>   avg = average([])
> except ZeroDivisionError:
>   print 'Hello world!'
> 

If you enclose statements in a try/except clause, and assuming no
exception is thrown, what are the runtime penalties?


Roey




More information about the Python-list mailing list