Exception Handling :: Python
Python has powerful exception handling capabilities. There are few exception which are present in BaseException class, and many exceptions are derived from the class Exception which is the child class of BaseException. All the user defined exceptions has to be derived from the class class Exception or its child.
Let's see exception SystemExit direct child of BaseException in action: import sys
Let's see exception SystemExit direct child of BaseException in action: import sys
try:
sys.exit()
except Exception as e:
pass
print(e)
In above case, the program exits regardless of Exception being caught.
That is because, System exit exception is derived from BaseException not Exception.
However, in following case; the exception is caught as expected:
try:
raise FloatingPointError
except ArithmeticError:
pass
print("Caught!") # o/p -> Caught!
The Last example shows you should be aware of exceptions hierarchy clearly :) :)
For complete references refer to following link and table:
https://airbrake.io/blog/python-exception-handling/class-hierarchyBaseException
+-- 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
sys.exit()
except Exception as e:
pass
print(e)
In above case, the program exits regardless of Exception being caught.
That is because, System exit exception is derived from BaseException not Exception.
However, in following case; the exception is caught as expected:
import sys
try:
sys.exit()
except BaseException as e:
pass
print(e) # o/p -> 'SystemExit'
Lets' see few awful things that can go wrong in Exceptions try/catch:
try:
raise "Value Error"
except "Value Error":
pass
print("UnCaught!") # TypeError: exceptions must be old-style classes or derived from BaseException, not str
try:
sys.exit()
except BaseException as e:
pass
print(e) # o/p -> 'SystemExit'
Lets' see few awful things that can go wrong in Exceptions try/catch:
try:
raise "Value Error"
except "Value Error":
pass
print("UnCaught!") # TypeError: exceptions must be old-style classes or derived from BaseException, not str
try:
raise ArithmeticError
except ArithmeticError:
pass
print("Caught!") # o/p -> Caught!
try:
raise ArithmeticError
except FloatingPointError:
pass
print("UnCaught!") # This is not printed & raises ArithmeticError exceptions
raise ArithmeticError
except ArithmeticError:
pass
print("Caught!") # o/p -> Caught!
try:
raise ArithmeticError
except FloatingPointError:
pass
print("UnCaught!") # This is not printed & raises ArithmeticError exceptions
try:
raise FloatingPointError
except ArithmeticError:
pass
print("Caught!") # o/p -> Caught!
The Last example shows you should be aware of exceptions hierarchy clearly :) :)
For complete references refer to following link and table:
https://airbrake.io/blog/python-exception-handling/class-hierarchyBaseException
+-- 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
Comments