An except block is the branch of a Python try statement that handles a matching exception instead of allowing it to terminate the program. Python checks except clauses in order and runs the first compatible one. section 14.2.3
Use these forms to control what the handler catches:
- Write `except ExceptionType:` to catch one exception type without binding the exception. section 14.2.3
- Write `except ExceptionType as error:` to bind the thrown exception for inspection or reporting. section 14.2.3
- Use a tuple such as `except (TypeA, TypeB):` when one block should handle several exception types. section 14.2.3
- Place narrower exception types before broader ones: because `IndexError` inherits from `LookupError`, a preceding `except LookupError` makes a later `except IndexError` unreachable for that error. section 14.2.7
- Use a bare `except:` only as a final catch-all; otherwise, an unmatched exception continues up the call chain. section 14.2.3
Handling file-processing cases
A file-processing loop can catch `IOError` when opening a file and a custom `EmptyFileError` when the file contains no text. The matching except block reports the problem, while `else` handles successful reads and `finally` runs for every file. section 3.4.7
try: body except exception_type1 as var1: exception_code1 except exception_type2 as var2: exception_code2 except (exception_type3, exception_type4 as var3: exception_code3 . except: default_exception_code else: else_body finally: finally_body
Use the complete syntax listing to compare typed handlers, tuple handlers, a default handler, and the optional else and finally clauses. section 14.2.3
try: body except LookupError as error: exception code except IndexError as error: exception code
Use the ordering example to see how inheritance determines which except block handles an exception. section 14.2.7

