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

Python

abhishek krishna vandadi
abhishek krishna vandadi
878 Points

I did not understand the exception handling code which was given as quiz question

print("A") try: result = "test" + 5 print("B") except ValueError: print("C") except TypeError: print("D") else: print("E") print("F")

1 Answer

Cruze Howard
Cruze Howard
444 Points

The code prints "A" as there are no parameters around it's execution. It then attempts to try and assign "test" + 5 to the variable result. This results in a TypeError as you can not add a string to an integer, thus skipping over the attempt at printing B. Since it is not a ValueError C is also not printed. The exception of a TypeError is occurring because of the attempt at "test" + 5 which then prints "D". Since an except was ran we skip over the else statement, as it only runs if none of the errors come up. "F" is then printed like "A" because of its lack of any parameters. Hope this helps!

print("A")
try:
    result = "test" + 5
    print("B")
except ValueError:
    print("C")
except TypeError:
    print("D")
else:
    print("E")
print("F")

RESULTS A D F