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

It looks to me like adding the 'as err' code on the exception results in ugly error if you enter a string instead of int

Now instead of 'Thats not a valid value', you get:

'Oh no thats not a valid value. Try again... could not convert string to float: 'whatever your string was'

Second line is ugly and confusing to a user. How would you cover this? You don't know what the err will be, so simply printing it out can be risky.

1 Answer

You could catch a specific kind of error like so:

try:
    # ... code goes here ...
except ValueError:
    print('Oh no that is not a number!')

The fourth line is only executed if the code in the try block causes a ValueError. Python returns a ValueError when you try to run something like int('a')