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 trialniveusth
4,528 PointsHow to find the "max allowed is 42" text from the MessageToLongError?
I couldn't fine the text "max allowed is 42" from the 2nd error so I just copied pasted it to my except statement.
7 Answers
Steven Parker
231,236 PointsThat's cheating! It's funny that it let you get away with that.
But the way to capture the built-in text is with the "as" operator:
except MessageTooLongError as err: # this copies the exception text into "err"
print(f"Oh no! Your message was too long ({err})")
Cao Tru
Courses Plus Student 1,103 PointsYes. That's cheating :)), the right code is:
except MessageTooLongError as err:
print("Oh no! Your message was too long ({})".format(err))
Steven Parker
231,236 PointsThat will work also. I used an f-string instead of "format" in my example as it makes the code a bit more compact, and to my eyes a bit easier to read. And to cover all the options, one other method that would produce the same results is a formatted string:
print("Oh no! Your message was too long %s" % err)
Cao Tru
Courses Plus Student 1,103 PointsThat great Steven! How can i know more option like f-string and % err, can you share something. Sorry because i just start learning coding
Steven Parker
231,236 PointsLearning the other "tools in the toolbox" will be a natural part of your progress as you continue learning. The earlier courses concentrate on showing you at least one way to do things, which will typically be the "classic" methods like "format".
For a definitive reference I use the online Python manual, but it's pretty dry reading. I'd recommend just sticking with the courses for now and if your curiosity is overwhelming, check out some of the conference videos.
Cao Tru
Courses Plus Student 1,103 PointsThanks for your advice Steven.
Norma Martinez Quiroz
3,651 Pointsexcept MessageTooLongError as error : print("Oh no! Your message was too long ${}".format(error))
Steven Lee
1,878 PointsHello!
May I ask a question about the default message set in the MessageToLongError
Could I change the message by raise the error variable again and put in the new message? I have tried this in the previous practice willing to change the default message in ValueError
, unfortunately, I failed.
nicolea
5,048 Pointsexcept MessageTooLongError as err:
print("Oh no! Your message was too long {}".format(err))
Jonathan Marus
1,755 PointsJonathan Marus
1,755 PointsDitto, I read it wrong I did .format(message) thinking text from exception was the original text from the user that caused the error. but it actually meant to print the error haha. so i cheated and just copied it into my print statement as well