try Statement with Multiple excepts
The except statement in such formats specifically detects exceptions named Exception. You can chain multiple except statements together to handle different types of exceptions with the same try:
except Exception1: suite_for_exception_Exception1 except Exception2: suite_for_exception_Exception2
This same try clause is attempted, and if there is no error, execution continues, passing all the except clauses. However, if an exception does occur, the interpreter will look through your list of handlers attempting to match the exception with one of your handlers (except clauses). If one is found, execution proceeds to that except suite.
>>> def safe_float(object): try: retval = float(object) except ValueError: retval = 'could not convert non-number to float' except TypeError: retval = 'object type cannot be converted to float' return retval >>> safe_float('xyz') 'could not convert non-number to float' >>> safe_float(()) 'object type cannot be converted to float' >>> safe_float(200L) 200.0 >>> safe_float(45.67000) 45.67