Overview of Exception Handling¶
Let us get an overview of Exception Handling.
- Exception Handling in general means dealing with unexpected events (eg: trying to add a number with string).
- Here are the different ways using which we handle exceptions in Python.
- Using try catch blocks.
- Throwing or raising exceptions as part of if conditions.
- Using assert function.
- Using
try catch
In [1]:
a = 1
In [2]:
b = '10'
In [3]:
a + b # TypeError is Exception Class. It is followed by exception message
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [3], in <cell line: 1>() ----> 1 a + b TypeError: unsupported operand type(s) for +: 'int' and 'str'
In [4]:
try:
res = a + b
except TypeError:
# Below statement will not print exception class name
print(f'Either {a} or {b} or both are not integers')
Either 1 or 10 or both are not integers
In [5]:
help(TypeError)
Help on class TypeError in module builtins: class TypeError(Exception) | Inappropriate argument type. | | Method resolution order: | TypeError | Exception | BaseException | object | | Methods defined here: | | __init__(self, /, *args, **kwargs) | Initialize self. See help(type(self)) for accurate signature. | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | ---------------------------------------------------------------------- | Methods inherited from BaseException: | | __delattr__(self, name, /) | Implement delattr(self, name). | | __getattribute__(self, name, /) | Return getattr(self, name). | | __reduce__(...) | Helper for pickle. | | __repr__(self, /) | Return repr(self). | | __setattr__(self, name, value, /) | Implement setattr(self, name, value). | | __setstate__(...) | | __str__(self, /) | Return str(self). | | with_traceback(...) | Exception.with_traceback(tb) -- | set self.__traceback__ to tb and return self. | | ---------------------------------------------------------------------- | Data descriptors inherited from BaseException: | | __cause__ | exception cause | | __context__ | exception context | | __dict__ | | __suppress_context__ | | __traceback__ | | args
In [6]:
try:
res = a + b
except TypeError as te:
# Print Exception class name along with custom message.
print(f'{type(te).__name__}: Either {a} or {b} or both are not integers')
TypeError: Either 1 or 10 or both are not integers
In [7]:
try:
res = a + b
except TypeError as te:
# Print default message along with custom message.
# We can also use str (class name will not be printed)
print(f'{repr(te)}: Either {a} or {b} or both are not integers')
TypeError("unsupported operand type(s) for +: 'int' and 'str'"): Either 1 or 10 or both are not integers
In [8]:
help(Exception)
Help on class Exception in module builtins: class Exception(BaseException) | Common base class for all non-exit exceptions. | | Method resolution order: | Exception | BaseException | object | | Built-in subclasses: | ArithmeticError | AssertionError | AttributeError | BufferError | ... and 15 other subclasses | | Methods defined here: | | __init__(self, /, *args, **kwargs) | Initialize self. See help(type(self)) for accurate signature. | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | ---------------------------------------------------------------------- | Methods inherited from BaseException: | | __delattr__(self, name, /) | Implement delattr(self, name). | | __getattribute__(self, name, /) | Return getattr(self, name). | | __reduce__(...) | Helper for pickle. | | __repr__(self, /) | Return repr(self). | | __setattr__(self, name, value, /) | Implement setattr(self, name, value). | | __setstate__(...) | | __str__(self, /) | Return str(self). | | with_traceback(...) | Exception.with_traceback(tb) -- | set self.__traceback__ to tb and return self. | | ---------------------------------------------------------------------- | Data descriptors inherited from BaseException: | | __cause__ | exception cause | | __context__ | exception context | | __dict__ | | __suppress_context__ | | __traceback__ | | args
In [9]:
int(a) + int(b)
Out[9]:
11
In [10]:
a = 1
In [11]:
b = 'ten'
In [12]:
int(a) + int(b)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [12], in <cell line: 1>() ----> 1 int(a) + int(b) ValueError: invalid literal for int() with base 10: 'ten'
In [13]:
try:
res = int(a) + int(b)
except ValueError:
print(f'Either {a} or {b} or both cannot be converted to integers')
Either 1 or ten or both cannot be converted to integers
- Throwing or raising exceptions as part of if conditions
In [14]:
if type(a) != int or type(b) != int:
raise ValueError(f'{a} or {b} or both cannot be converted to integers')
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [14], in <cell line: 1>() 1 if type(a) != int or type(b) != int: ----> 2 raise ValueError(f'{a} or {b} or both cannot be converted to integers') ValueError: 1 or ten or both cannot be converted to integers
- Using
assert
In [15]:
age = -1
assert age >= 0, f'Age ({age}) cannot be negative'
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) Input In [15], in <cell line: 2>() 1 age = -1 ----> 2 assert age >= 0, f'Age ({age}) cannot be negative' AssertionError: Age (-1) cannot be negative