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
Michael Pastran
4,727 PointsWhat does he mean when he says "blows up" im completly confused with the try: block except: block part of this video.
when the teacher says "blows up" does he mean when we get an error? idk im confused can anyone explain the exceptions more with more lamer terms lol. thank you
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsBy "blows Up" he means when the code has an error (an "exception") that isn't handled by the code itself.
To handle exception, that is keep your code from blowing up, you can handle the error within your own code by use the try/except statement. For example, if you want to open a file but the file might not exist, you can wrap the open statement in a try/except then if the file can't be open, your code can take other actions rather than crash.
Some languages prefer you to test for a file's existance before trying the open. Python says 'Go For It!" and just handle the consequences using a try/except block.
Try/excepts can have four parts.
- try -- (required) a block of code to, well, try
-
except -- (required) block of code that will immediately execute if something goes wrong in the
tryblock. It is customary to state the exception you are concerned about. For the file missing file case the exception would be FileNotFoundError. Python has many built-in exceptions. -
else -- (optional) block of code that executes if no errors occur in
tryblock -
finally -- (optional) block of code that executes following either the
exceptor theelseblock
More try/except info here
A 'try/except` statement protects your code anywhere in situations where you don't have control of the situation because your dealing with items outside your code (user input, OS interactions, the return value of other code).