python exception error code code example

Example 1: exception types python

BaseException
   ] SystemExit
   ] KeyboardInterrupt
   ] GeneratorExit
   ] Exception
        ] StopIteration
        ] StopAsyncIteration
        ] ArithmeticError
        |    ] FloatingPointError
        |    ] OverflowError
        |    ] ZeroDivisionError
        ] AssertionError
        ] AttributeError
        ] BufferError
        ] EOFError
        ] ImportError
        |    ] ModuleNotFoundError
        ] LookupError
        |    ] IndexError
        |    ] KeyError
        ] MemoryError
        ] NameError
        |    ] UnboundLocalError
        ] OSError
        |    ] BlockingIOError
        |    ] ChildProcessError
        |    ] ConnectionError
        |    |    ] BrokenPipeError
        |    |    ] ConnectionAbortedError
        |    |    ] ConnectionRefusedError
        |    |    ] ConnectionResetError
        |    ] FileExistsError
        |    ] FileNotFoundError
        |    ] InterruptedError
        |    ] IsADirectoryError
        |    ] NotADirectoryError
        |    ] PermissionError
        |    ] ProcessLookupError
        |    ] TimeoutError
        ] ReferenceError
        ] RuntimeError
        |    ] NotImplementedError
        |    ] RecursionError
        ] SyntaxError
        |    ] IndentationError
        |         ] TabError
        ] SystemError
        ] TypeError
        ] ValueError
        |    ] UnicodeError
        |         ] UnicodeDecodeError
        |         ] UnicodeEncodeError
        |         ] UnicodeTranslateError
        ] Warning
             ] DeprecationWarning
             ] PendingDeprecationWarning
             ] RuntimeWarning
             ] SyntaxWarning
             ] UserWarning
             ] FutureWarning
             ] ImportWarning
             ] UnicodeWarning
             ] BytesWarning
             ] ResourceWarning

Example 2: catch error data with except python

import sys
try:
	S = 1/0 #Create Error
except: # catch *all* exceptions
    e = sys.exc_info()
    print(e) # (Exception Type, Exception Value, TraceBack)

############
#    OR    #
############
try:
	S = 1/0
except ZeroDivisionError as e:
    print(e) # ZeroDivisionError('division by zero')