Intro
This tutorial is aimed to provide knowledge on how to handle exceptions (errors) in Python 3. It will assume you have an understanding of common Python functionality.
Requirements
- Python 3 Knowledge, if not start here
What are they?
When making Python programs you may have encountered “unhandled exceptions”. Well in Python these are errors that have happened during runtime. Exceptions in Python are all instances of the BaseException class, this allows the programmer to write code to handle exceptions as well as create their own.
Handling Exceptions
First we will need to know how to handle exceptions. The example code that is shown below will create a divide by zero exception.
|
|
When this is run you will get a similar output to what is shown below.
|
|
As you can see when we run this we do not get the output of the result. This is because when an exception is raised it will cause the program to crash. You may also notice that we get a error message, this “traceback” is telling us what line, file if we had multiple and the exception that was raised. This will come in handy when you have large amounts of code to quickly debug and find what code is causing your program to not work.
You could imagine that the example code is actually a user trying to enter a number into a calculator.
Try Except
Now we have an error lets see how to handle the exception so that the user will see a friendly error message.
|
|
As you can see from the code we use “try” and “except” keywords. In the try block we place code that may raise an exception. Then in the “except” block we place code that will handle an exception. In our case the exception will be ZeroDivisionError
.
There are many more built into Python, check them out at the Python docs
Now when this code is run we will get the following output:
|
|
If we were writing a program having a friendly error message is better than the program crashing. If we were asking for a users input we could display a message and then ask for the input again.
We could also omit the ZeroDivisionError
. this will then catch all errors.
|
|
The “pass” keyword tells Python to skip execution of the current line.
We can also catch multiple using:
|
|
If we wanted to run different code when different exceptions are raised we can use multiple “except blocks”.
|
|
Try Except Finally
Now lets add another feature of the try except clause. The “finally” keyword allows us to run code either after an exception or after a successful run of the try block.
|
|
Try Except Else Finally
Now lets use another feature. The “else” keyword allows us to run code when an exception has not been raised.
|
|
Try Finally
We can also omit the the except block and just have a try and finally statement. This allows us to run code after an exception has occurred however not handle it. This could be used for example: closing access to a file.
|
|
Raising Exceptions
Now lets raise an in-built exception in Python. We can do this using the “raise” keyword.
|
|
We could also give a message argument:
|
|
Custom Exception
When we want to create our own exception we can inherit from the Exception
which is built-in. We can also inherit from existing exceptions like ValueError
. It is not recommended to inherit from BaseException
by the official Python documentation.
|
|
“Safe” Calculator
Now lets put our knowledge to use. We are going to build the calculator we built in previous tutorials, however this time we are going to handle possible errors.
- Get two numbers from the user as separate variables
- convert both to int data-types
- handle ValueError
- divide numbers together
- handle ZeroDivisionError
- output the answer
Try to code your own version before looking at the completed code below.
|
|
Now lets test our application. We will first test it with a invalid integer.
We should see the following output when run:
|
|
Lets test dividing by zero. We should get the following output:
|
|
As you can see our error messages work, they can handle entering invalid integers or prevent the user from dividing by zero.
Research
There are more features when handling exceptions that are not shown in this tutorial.
I would recommend researching re-raising errors with context using the “from” keyword. The code would look similar to what is shown below:
|
|