Title: Use a try block's else statement in Python
My post Use a loop's else block in Python explains how to use an else clause in a for loop. The try except block also has an optional else section that executes if the try section ends normally. In other words, it executes if you don't break out of the try section with some weird shortcut like an exception, break statement, or return statement.
This example lets you test the try block's else section.
Testing Try
The following example code uses a loop to let you test various ways of leaving a try block.
# Test various ways to exit a try block.
while True:
try:
print('\n1 = normal, 2 = except, 3 = break')
command = input('> ')
if command == '2':
raise Exception('Oopsie daisy!')
elif command == '3':
break
print(' Try')
except Exception as e:
print(f' Caught exception: {e}')
else:
print(' Else')
finally:
print(' Finally')
The code uses a while loop so you can try different ways of leaving the try section. Inside the loop, it enters a try block and prompts you for an input.
If you enter 1, the code moves through the code normally. The except section does not execute but the else and finally sections do. Here's the prompt and the program's output for this case.
1 = normal, 2 = except, 3 = break
> 1
Try
Else
Finally
If you enter 2, the program raises an exception, control jumps out of the try section into the except section, and the program displays an exception message. Because the code did not finish the try section normally, the else section is skipped and the finally section prints its message. Here's the prompt and the program's output for this case.
1 = normal, 2 = except, 3 = break
> 2
Caught exception: Oopsie daisy!
Finally
If you enter 3, the code executes a break command to break out of the while loop. The try section did not end normally so the program skips the else section. The finally section executes because it always does. Here's the prompt and the program's output for this case.
1 = normal, 2 = except, 3 = break
> 3
Finally
Conclusion
Now that you understand the try block's else section, you may wonder why you should bother. For example, you could just put that code at the end of the try section. If there's an error or some other escape from the try section, that code isn't executed, just as it isn't if you put the code in the else section.
The difference is that the code inside the else section is not protected by the try block, so if it raises an error, the program crashes. (Unless you have an outer layer of try except protection.) That lets you find errors so you can fix them.
You can always avoid using the else section by using appropriate if statements, but that may make your code more confusing. If you snoop around the discussion boards for a bit, you'll find lots of arguments about this statement but few examples that are super convincing.
You can avoid using this statement by using appropriate if tests, so it's not absolutely necessary. The best advice I can give is to use it if it makes your code easier to read.
Download the example to experiment with it.
|