python get exception type code example
Example 1: exception pyton print
except Exception as e: print(e)
Example 2: print type of exception python
try:
someFunction()
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print (message)
Example 3: catch error python
import traceback
dict = {'a':3,'b':5,'c':8}
try:
print(dict[q])
except:
traceback.print_exc()
# This will trace you back to the line where everything went wrong.
# So in this case you will get back line 5
Example 4: 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 5: python: raise error
class MyError(TypeError):
pass
raise MyError('An error happened')