Using return With try and finally Blocks
For more information on topics covered in this lesson, check out these resources:
00:00
In Python, when you write code that you know may cause an exception, you typically use it with a try
statement. One of the features of a try
statement is an optional finally
clause, which is executed whether or not there was actually an exception.
00:15
Real Python has an introduction to exceptions tutorial, where you can read more information about the try
statement. In this lesson, we’ll be looking specifically at using the return
statement and try
statements that have a finally
clause.
00:32
Simply put, when you use a return
statement inside a try
statement that has a finally
clause, that finally
clause is always executed before the return
statement. Specifically, this means that the finally
clause will always be executed when the try
statement is encountered.
00:52
So even though the finally
clause may appear after one or more return statements, it should not be viewed as dead code. Here is an example of a try
statement.
01:04
This function takes a parameter, value
, and attempts to convert it to a floating-point value. If successful, that floating-point value will be returned.
01:14
If not, a string will be returned. But before returning, the print statement and the finally
clause will be executed.
01:24
Here’s that same function with a single parameter, value
. The function will attempt to convert the argument provided for value
into a floating-point value.
01:38
If that argument is not something that represents a floating-point value, then the call to float()
will cause an error, so this function call is placed in a try
statement.
01:50
If the statement is successful, then the floating-point value it represents will be returned. If it is unsuccessful, then a ValueError
will occur, and the return
statement in the except
clause will be executed, which returns the argument in values
as a string.
02:13
In either case, since this try
statement has a finally
clause, it will be executed before the function returns. So for any argument in value
, we will see the phrase Run this before returning
printed before the function ends. And in an interactive session, we’ll see the return value after that print statement.
02:38 So if I try to run this, we will see the print statement for any argument provided to the function and then the appropriate return value is returned to the calling environment.
02:48 Let’s check this out. We’ll import this function.
02:55
Let’s first try this out with an argument for which the float()
function will succeed.
03:02
We can see the integer 9
was converted to the floating-point value 9.0
and returned, but this was displayed after the message Run this before returning
.
03:14 Here’s one where it will fail.
03:19
First, we see the Run this before returning
message, and here, 'one'
was returned as a string. We hit the except
part of this try
statement.
03:35
So remember that any code that is in a finally
clause will always be executed before any return
statements in either the try
or except
clauses. In your last lesson, we will look at using the return
statement in generator functions.
Become a Member to join the conversation.