exceptions beyond errors?

W Isaac Carroll icarroll at pobox.com
Mon Jun 9 09:51:03 CEST 2003


e y suzuki wrote:
> i'm curious to know about ways that one could use exceptions
> in python programs aside from just detecting errors.  so far
> i' have read that they can be useful when a large amount of 
> processing may need to be discarded, such as with branch-
> and-bound and parsing algorithms.
> 
> are there additional non-error-detection cases where exceptions
> are useful?

I've seen examples where they're used to exit early from multiple nested 
loops, such as:

try:
     for a in a_list:
         try:
             for b in b_list:
                 for c in c_list:
                     if somecondition:
                         raise LeaveInnerLoops()
                     if othercondition:
                         raise LeaveLoopsEntirely()
         except LeaveInnerLoops:
             pass
except LeaveLoopsEntirely:
     pass

TTFN






More information about the Python-list mailing list