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!
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
Swallowing errors? I don't get what he means.
I don't quite understand @ 2:30 what he means by
It will swallow all errors? I remember him saying that if you wrote
then if someone got a
it wouldn't be caught. So wouldn't the
be useful since any random instance of some error would be caught instead of the term he used "swallowing" whatever that is?
Chris FreemanTreehouse Moderator 68,404 Points
Great question! The except block is used to handle errors from the try block. When an exception class is provided, the except block will handle only that exception type. You can have multiple except blocks for a try statement.
If an exception is not handled, the error is automatically reraised up the call chain. If the calling function doesn't handle it, it gets reraised again, and so on until the top level where the stacktrace of errors is displayed from each reraising function and the program stops.
If no exception class is listed on the
except line, then all errors will be handled by this except block. This effectively "swallows"/consumes/hides all errors preventing any error from being reraised and handled or seen outside of the current local block.
Post back if you need more help!
You are right. If you use *except: *, all errors get caught.
This seems great. However, it lacks precision. It does not tell you (the developer) what went wrong. This is why it is generally advised to go for specific except blocks. That way you know what went wrong, and can target your response accordingly.
When he says swallows, the code will take in any input(numbers, words, ect) that the user inputs. So if there is no except block the user can input whatever they want and the code will take it(swallow) and the developer(aka you) will have to deal with a bunch of inputs that you didn't want in the first place. I hope I explained that correctly! Good luck!!