This article is a part of a 30 day Python challenge series. You can find the links to all the previous posts of this series here
No program is perfect. Unless it is a hello world program written with great care π
Today I explored all about handling errors in Python, which I will try to explain in plain and simple words. Handling errors is probably the most important concepts of programming.
All applications are prone to errors a.k.a bugs or exceptions. Errors are not evil. Errors most of the time convey useful messages when a program fails to run or execute for whatever reason. It can be an accidental tying mistake, maybe the application is offline while it is trying to access the internet. Maybe there is an error while calculating, or maybe the program runs out of memory. There are a lot of possible errors that might happen when we write a program. When an error is detected, the program just terminates. This may or may not something we want. Rather than expecting code to run without errors, the better and more pragmatic approach is to handle errors and accordingly take some actions.
In Python, errors can be broadly segmented into two - Syntax Errors and Exceptions.
Syntax Errors occur when we write some Python code which the Python interpreter is unable to validate as correct Python code.
Whenever we run into syntax errors, the Python interpreter does a very good job by providing in detail which line caused the error so it can be corrected.
The interesting part is about the other kind of error that might occur apart from a syntax error.
Our programs are often connect to the outside world to receive certain values and perform computations based on those values. There is always a probability that those values are not correct. In that case, an error would occur. Thus they need to be handled.
- age = input('Enter your age')
- if age > 18:
- print('You are an adult')
- else:
- print('You are a minor')
Running this simple code will result in an exception.
- Traceback (most recent call last):
- File "main.py", line 2, in <module>
- if age > 18:
- TypeError: '>' not supported between instances of 'str' and 'int'
The exception is a TypeError because the value of age is a string. So to prevent it, age needs to be type-casted or converted to an integer.
- age = int(input('Enter your age'))
- if age > 18:
- print('You are an adult')
- else:
- print('You are a minor')
Now, if the user provides a string value instead of a number, there will be another exception.
- Enter your age asdf
- Traceback (most recent call last):
- File "main.py", line 1, in <module>
- age = int(input('Enter your age'))
- ValueError: invalid literal for int() with base 10: 'asdf'
Try except block
To prevent such an error, Python provides a try except block, where the action is placed inside the try block and if there is an exception, the except block will be executed.
As a comparison, in the JavaScript universe, there is a try catch block. Python just has a different name for catch. The functionality is exactly the same.
- try:
- age = int(input('Enter your age'))
- if age > 18:
- print('You are an adult')
- else:
- print('You are a minor')
- except:
- print('Invalid value provided')
Else block
Besides the try except block, Python also provides an else block which gets executed if there is no exception in a try except block of code. It is an extension to the try except statement.
- try:
- age = int(input('Enter your age'))
- if age > 18:
- print('You are an adult')
- else:
- print('You are a minor')
- except:
- print('Invalid value provided')
- else:
- print('Thank You!')
Finally block
Often while writing a program, we would want to perform an action irrespective of whether there was an exception or not. An example can be sending log messages to a server. Python provides a finally block as a part of the try except block which gets executed whether or not there is an exception.
- try:
- age = int(input('Enter your age'))
- if age > 18:
- print('You are an adult')
- else:
- print('You are a minor')
- except:
- print('Invalid value provided')
- finally:
- print('Sendiing dummy log to server')
Built-in Exceptions
Python provides a lot of built-in exceptions which are basically instances of the BaseException class.
These exception classes can be used to handle specific exceptions such as TypeError, ValueError, SystemError etc.
- from functools import reduce
-
- def calc_average(number_list):
- ''
-
-
- sum = reduce(lambda acc, curr: acc + curr, number_list)
- average = sum/len(number_list)
- return average
-
- print(calc_average([1,2,3,4,5]))
-
-
- print(calc_average(['1','2','3','4','5']))
- print(calc_average(None))
- print(calc_average(3/0))
To make the function more defensive, we can catch (handle) those specific exceptions.
- from functools import reduce
-
- def calc_average(number_list):
- ''
-
-
- try:
- sum = reduce(lambda acc, curr: acc + curr, number_list)
- average = sum/len(number_list)
- return average
- except TypeError:
- print('Only a list of numbers is valid')
-
-
- print(calc_average('hello world'))
The above code handles invalid arguments. However, there can be defects in the logic itself. It can also be handled using separate except blocks.
- from functools import reduce
-
- def calc_average(number_list):
- ''
-
-
- try:
- sum = reduce(lambda acc, curr: acc + curr, number_list)
- average = sum/len(number_list)
- 1.0/0
- return average
- except TypeError:
- print('Only a list of numbers is valid')
- except ZeroDivisionError:
- print('cannot divide by zero')
-
-
- print(calc_average([1,2,3,4,5]))
Multiple exceptions can be handled together in a single except block as well.
- from functools import reduce
-
- def calc_average(number_list):
- ''
-
-
- try:
- sum = reduce(lambda acc, curr: acc + curr, number_list)
- average = sum/len(number_list)
- return average
- except (TypeError, ZeroDivisionError):
- print('Only a list of numbers is valid')
-
- print(calc_average(['asdfasdf']))
The code written above handles possible exceptions but it is hiding the useful stack trace errors provided by the interpreter which tells precise which line is responsible for the code.
The actual error message as well a custom error message can be combined together.
- from functools import reduce
-
- def calc_average(number_list):
- ''
-
-
- try:
- sum = reduce(lambda acc, curr: acc + curr, number_list)
- average = sum/len(number_list)
- return average
- except TypeError as type_error:
- print(f'Only a list of numbers is valid {type_error}')
- except ZeroDivisionError as zero_div_error:
- print(f'cannot divide by zero {zero_div_error}')
-
- print(calc_average('hello world'))
-
This makes the error messages more useful. It is also possible to raise custom error based on some condition using the raisekeyword in Python.
- name = input('Enter your name')
- if name.lower() == 'god':
- raise('Name cannot be GOD!')
- else:
- print(name)
It is not possible to memorize all the Python built-in exception classes unless you are a Savant!
The Python built-in exceptions doc can be used as a reference to handle desired exceptions.
References
- https://realpython.com/python-exceptions/
- https://www.programiz.com/python-programming/exception-handling
- https://docs.python.org/3/tutorial/errors.html
That’s all for today! An important topic explored that really help in writing more resilient Python code. Tomorrow I shall explore another advanced Python topic - Generators.
>Have a great one!