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 Expecting Exceptions

Using Except when name of errors is not known.

Here, we are using "except" when we know the name of the errors from the console. Is there an alternative when we don't know the name of the Errors??

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

There is an Exception Hierarchy so you can always specify a "parent" type that covers all of the sub-types. You can always use Exception to catch most errors. This will catch all errors but SystemExit, KeyboardInterrupt, `GeneratorExit.

It's best to find out the specific error you concerned about, so other errors aren't hidden.

You can find out which error is being caught by use as err to capture the error:

>>> try:
...     'a' + 5
... except Exception as err:
...     print(err.__class__)
<class 'TypeError'>

Post back if you need more help. Good luck!!