Using the try and except Keywords
And what this will do is that it will catch any
ValueErrors that come out, and instead of raising that to the top level and stopping your program, it will catch it, and then it will print
"That was not an integer".
It will run whatever’s in the indented block after the colon So to summarize that, you have
try: and then an indented block, and then
except with the type of error, colon, and then another indented block.
So it’s going to try to run what’s in the
try indented block, and if it runs into an error and there is an
except with the type of error, then it’s going to run whatever’s in that indented block, instead of just raising the error up to the top.
This example is quite common to see because you’re taking user input with the
input() function, and you’re asking someone to enter an integer, and now they might enter
"hello", which would raise a
Thankfully, you’ve caught that
ValueError, and instead of just raising this big stack trace that might not be legible to someone who doesn’t do any programming, it will just print
"That was not an integer".
This allows you to customize the behavior of your program for each type of error. So if someone tries to use this function with
"hello" and a number, then they’d get a
TypeError, and the result from that would be the execution of the
print() function, which would print
"Both arguments must be numbers".
So line 1, you have
try with the colon and then line 2, you have the indented block with
1 / 0. On line 3, you have
except zeroDivisonError with a colon. And in that indented block, you have
print("I caught the error"). And on the line 6, finally, unindented, you have
print("done"). Just run this with a F5, and as you can see it prints
"I caught the error" and
"done". You’ll note that anything after the
1 / 0 will not get printed.
So if you just add a call to the
print() function after the
1 / 0 in the
try indented block, you’ll see that that doesn’t get printed because once the error is raised, that indented block is done for.
And you’ll also note that the error doesn’t stop the whole program. So apart from printing
"I caught the error" on line 5 in the indented block of the
except statement, then on line 7, you have an unindented
print("done"), which always prints.
It will just stop the program in its tracks and give you a traceback with the
ZeroDivisionError, which can be quite unreadable if you don’t have any experience or any inclination to learn programming, and you might be designing programs for your friends or colleagues to use, and they might not have any knowledge of programming.
There is a bit of a cheat code for the
except block, and that’s using a bare
except clause. You can use the
except keyword without any error class after it. As you can see, there’s the
except, and then immediately followed by the colon with no error class in between them.
If you default to using bare
except clauses with generic error message, and your program grows, you’ll end up seeing these error messages a lot, and they won’t give you any clues as to what went wrong.
So if there is a problem in your program, it’s going to be very hard to debug. So in the same way that you should give your variables descriptive names, handle errors explicitly to save your future self and colleagues a lot of headaches. In this lesson, you’ve had the rundown on the
except structure, and you’ve learned how to handle exceptions gracefully with this structure.
Become a Member to join the conversation.