Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
chailatte
941 PointsWhy purpose does the else statement serve in try/except if code runs fine?
Once the try/except combo is entered in this code to handle exceptions, what purpose does the else statement serve? The code runs fine without it. Tried looking this up myself in the official documentation (https://bit.ly/2ubE1Lw) which reads:
"The try ... except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception.
The use of the else clause is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasn’t raised by the code being protected by the try ... except statement."
If I understand this correctly, there could be other errors that could popup that are different from the error specified in the try-except portion. If by looking at the code below, an error other than an IndexError were to occur than the program would jump down to the else statement without the program breaking. Is this correct?
sodas = ["Pepsi", "Cherry Coke Zero", "Sprite"]
chips = ["Doritos", "Fritos"]
candy = ["Snickers", "M&Ms", "Twizzlers"]
while True:
choice = input("Would you like a SODA, some CHIPS, or a CANDY? ").lower()
try:
if choice == 'soda':
snack = sodas.pop()
elif choice == 'chips':
snack = chips.pop()
elif choice == 'candy':
snack = candy.pop()
else:
print("Sorry, I didn't understand that.")
continue
except IndexError:
print("Sorry. We are all out of {}.".format(choice))
else:
print("Here's your {}: {}.".format(choice, snack))
1 Answer
Steven Parker
243,318 PointsIf some other exception were to occur, the program would end. The "else" code is only executed if no exception occurs.
The difference between the "if" block and "else" block is that only exceptions that occur in the "if" block will be caught.
chailatte
941 Pointschailatte
941 PointsSorry I should've clarified. Originally, when this script didn't have the try/except statements the last print statement was part of the try block and followed the other else statement. After adding the try/except statements, this print statement was moved into its own else statement. Why is this better than the original?
Steven Parker
243,318 PointsSteven Parker
243,318 PointsAs I mentioned, the final "print" would not be protected by the "try". Since another IndexError seems unlikely, there's probably no practical difference between locating it there or at the end of the "try" block. But from a "best practice" standpoint, it's always good to keep only the code that needs exception catching inside the "try".