In python, we handle exceptions using a block called a try-except which wraps around our code. Python uses the try statement to attempt to run the code and uses the except statement if the code returns some sort of error.
try-except
try
except
The python documentation provides details on how to implement this. As before we will start with a simple example to illustrate how this works. In this example, we are going to generate a TypeError and handle it with a try-except block.
TypeError
lab_9_step_1_errors.py
integer = 50 string = "The number is" print(string + integer)
Hopefully, you can see the issue with this code.
To run the program, enter the following command in your terminal:
python lab_9_step_1_errors.py
Traceback (most recent call last): File "errors_lab_9_step_1.py", line 6, in <module> print(string + integer) TypeError: must be str, not int
The code returns a TypeError because you cannot add a string value to an integer without first telling python to convert the integer into a string using str()
str()
With this example, we are going to add the try-except block.
import logging integer = 50 string = "The number is" try: print(string + integer) except TypeError as err: logging.warning("Error - {}. You cannot add a string to an integer, without converting the integer to a string first".format(err))
This should return:
WARNING:root:Error - must be str, not int. You cannot add a string to an integer, without converting the integer to a string first
We have imported logging.
We have added a try-except block to our code.
In the except we have specified what action to take in the event of a TypeError.
We have provided a logging.warning() statement that returns the error message, and also some more meaningful information.
logging.warning()
Because we have not provided logging with a logging.basicConfig() it is returning the logging to the console.
logging.basicConfig()
First, the try clause (the statement(s) between the try and except keywords) is executed.
If no exception occurs, the except clause is skipped and execution of the try statement is finished.
If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement.
If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops.
The except statement above will only work for a TypeError because we have specified this in the except statement.
There can be multiple except statements which provide details on what to do in the event of different types of error.
Here is an example:
import logging integer = 50 string = "The number is" try: print(string + integer) except TypeError as t_err: logging.warning("Error - {}. You cannot add a string to an integer, without converting the integer to a string first".format(t_err)) except ValueError as v_err: logging.warning("Error - {}. Your message".format(v_err))
We want to be able to see exceptions generated by the AWS services we are calling. These are returned by Boto3 using botocore. We need to add an import statement into our code and then we can use the try-except statement. An example is shown below:
botocore
import logging import boto3 from botocore.exceptions import ClientError try: client = boto3.client('translate') <snip> except ClientError as e: logging.warning("<your msg> {}".format(e))
In this step, we have learned how to use the try-except statements to add exception handling to our code. We have also learned how to get boto3 to give us error messages. In the next step, we add try-except to our main code.