zerodivisionerror python code example
Example 1: zerodivisionerror python
# Example of a ZeroDivisionError:
number = 43/0
print(number)
"""
You can not divide by zero in Python, otherwise a rather unpleasant and
hard-to-read message comes up. To prevent this sort of thing from happening,
use the try - except function.
"""
try:
print(43/0)
except ZeroDivisionError:
print("Dividing by zero is not allowed!")
"""
This way, Python will print the answer, UNLESS there is a ZeroDivisionError, in
which case Python, instead of printing the error message, will print that you
cannot divide by zero.
"""
Example 2: 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 3: type error in python
try:
...
except SomeException:
tb = sys.exc_info()[2]
raise OtherException(...).with_traceback(tb)