Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,064 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!!