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 trialChristian Vasquez
Python Development Techdegree Student 820 PointsWhen to use "as err" for an exception
I'm having trouble knowing when to use the "as err" for an exception. For the section "Practice Creating and Using Functions in Python", I'm doing the 2nd objective in "Use an External Function", and I didn't use the "as err" for the "CommunicationError" and my work got accepted. But when I moved on to the 3rd objective, I had to use the "as err" for the "MessageTooLongError" because my work wasn't being accepted without it. Is it safe to say that I should just use the "as err" every time I have an exception?
try:
tweet(message)
except CommunicationError:
print("An error occurred attempting to connect to Twitter. Please try again!")
except MessageTooLongError as err:
print("Oh no! Your message was too long (...){}".format(err))
1 Answer
Chris Freeman
Treehouse Moderator 68,454 PointsGood question! You would use the as err
when you needed to access the object containing information about the error.
In the first case, you only needed to know that the error was raised and no specifics about what caused it.
In the second case, you wanted to print the specific message detailing the error. For this second case, you needed a reference to the error object so as err
was used.
Note that err
is just a name chosen. You could use any legal variable name here. You might see some code use as e
.
Post back if you need more help. Good luck!!