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 Python Basics Functions and Looping Raising Exceptions

Kohei Ashida
Kohei Ashida
4,882 Points

Why is the "err" variable needed here to display the message in ValueError argument?

I understand that raise is to define what kind of error to raise, and the text to print to the user but why is the "err" variable is needed here in print("({})".format(err))? I thought it's just enough to use "ValueError" like print("({})".format(ValueError)).

1 Answer

Steven Parker
Steven Parker
229,732 Points

You should try your suggested code as an experiment, you should see an output like "(<class 'ValueError'>)".

That's because "ValueError" is the class of the exception, and the variable (in this case "err") is a specific instance of the class which contains the message passed in when it was created during the "raise". It has a "__str__" override that returns the message when you reference the instance as a string.

Kohei Ashida
Kohei Ashida
4,882 Points

I got it! thank you for the quick response:)