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

Mark Johnson
Mark Johnson
7,171 Points

Challenge 3 of 3 on Practice Creating and using functions in python challenge

Here is the challenge its giving.

*BEGIN CHALLENGE TEXT*

Unfortunately Twitter can change the maximum amount of characters allowed at any time. The only thing we know is that a MessageTooLongError will be raised if that happens. The exception has additional messaging, so let's catch the exception and print it out: "Oh no! Your message was too long (...)" Where ... is the text from the exception.

*END CHALLENGE TEXT*

Here is my code but i get this error "Bummer: TabError: inconsistent use of tabs and spaces in indentation" plus im not sure if the code even correct. Can anyone help me figure out what im doing wrong?

*BEGIN CHALLENGE CODE*

""" This is importing a function named tweet from a file that we unfortunately don't have access to change.

You use it like so:

tweet("Hello this is my tweet")

If the function cannot connect to Twitter, the function will raise a CommunicationError If the message is too long, the function will raise a MessageTooLongError """ from twitter import ( tweet, MessageTooLongError, CommunicationError, )

message = input("What would you like to tweet? ") try: tweet(message) If MessageTooLongError: raise MessageTooLongError except MessageTooLongError: print("Oh no! Your message was too long (" + MessageTooLongError + ")") except CommunicationError: print("An error occurred attempting to connect to Twitter. Please try again!")

*END CHALLENGE CODE*

4 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

Since you got through Challenge 2 of 3, you understood how to catch errors. The cool thing about Python is that you can catch multiple exception types with the try/except block.

The subtle part is that the challenge calls for you to print out the error message that comes from the Exception class and put it inside in the parenthesis. I am putting the Exception into the variable "e".

This can be done in several ways, but I demonstrate the most basic way to do it.

Good luck on your Python journey!

try:
    tweet(message)
except CommunicationError:
    print("An error occurred attempting to connect to Twitter.  Please try again!")
except MessageTooLongError as e:
    print("Oh no! Your message was too long ({})".format(e))
Mark Johnson
Mark Johnson
7,171 Points

oh ok.. i get it now. Thanks a bunch for the help. I tried part of that i just didnt do the format(e) or anything else. that was most of my problem.

I'm facing the same problem my code is failing to run. try: tweet(message) #try CommunicationError: #print("An occurred in attemping connecting to Twitter") except CommunicationError: print("An occurred in attemping connecting to Twitter") if CommunicationError: print("An error occurred attempting to connect to Twitter. Please try again!") except MessageTooLongError as e: print("Oh no! Your message was too long ({})".format(e))

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

The Challenge is being used to illustrate intermediate error handling. The print messages are meant for the Challenge grader to read.

The "as err" or "as e" is a way that Python can take an instance of the Exception object class and give it to the user for deeper inspection, printing, or logging. In our case, we simply want the error string to print. That's why we used the .format() method of the string.

Some objects can produce very informative errors, some not so much. The ones that aren't informative have to be handled with more care if we want to have robust programs that won't just die when these encounter a runtime error, but might log or display the error-- allowing the user to correct the error or simply move on.

From the Python documentation:

The except clause may specify a variable after the exception name. The variable is bound to an exception instance with the arguments stored in instance.args. For convenience, the exception instance defines __str__() so the arguments can be printed directly without having to reference .args. One may also instantiate an exception first before raising it and add any attributes to it as desired.

https://docs.python.org/3/tutorial/errors.html

I still don't understand the "as err" or "as e" thing... what's the difference with the CommunicationError message? Why adding parenthesis to the MessageTooLongError message? I am missing something :/

Bonnie McInturf
Bonnie McInturf
2,177 Points

Just documenting that this worked, thanks to Jeff Muday on Dec 13, 2018:

CommunicationError = "An error occurred attempting to connect to Twitter. Please try again!"


message = input("What would you like to tweet?  ")

try: 
    tweet(message) 

except MessageTooLongError as e:
    print("Oh no! Your message was too long ({})".format(e))
except: 
    print(CommunicationError)