Course 2.1Lesson 2 of 5
With try and except you say: try these lines, and if this kind of error happens, do this instead. The program does not stop. An except part runs only when an error of the named kind happens, and you can write several, one for each kind, or name two kinds in a tuple. as error gives the error itself. else runs only when there was no error, and finally always runs. Always name the error you expect. A bare except catches every error, including your own typing mistakes, and hides real bugs.
Try the code and the simulations beside the lesson.
After this lesson you can
try and except, so that the program goes on,except parts, and name the error you expect,else and finally, andexcept: is dangerous.With try and except you say: "Try these lines. If this kind of error happens, do this instead."
Predict first: what will this show? Does the last line run? Then run it.
The conversion int("12a") fails with a ValueError. Python does not stop. It jumps out of the try lines, to the except ValueError: lines, and runs them. When they are done, the program goes on with the first line after the whole block. So the last line runs too.
Notice what did not run: the line print("It worked:", number). The jump happened at the failing line, and the lines after it, inside the try, are skipped.
In the output, (the division was tried) appears twice, once for each call. And the order is interesting: the finally message appears before the answer is printed, because the function's finally runs when the function ends, before the caller prints the value that was returned.
You can write several except parts, one for each kind. Python takes the first one that matches.
The last loop shows two more tools.
except (ValueError, TypeError) as error: names two kinds in a tuple. One part handles both.as error gives the error itself, a value that you can print or look at. type(error).__name__ is the name of the kind.int(None) is a TypeError, and int("abc") is a ValueError. The two parts of the tuple catch them both.After the except block, the program goes on normally. The error is handled, so nothing stops.
Name the error that you expect: except ValueError:. Why? Look at this program.
If the part had said except ValueError:, the typing mistake (a NameError) would not have been caught, and Python would have stopped with a clear message that shows the line.
The rule: catch the errors that you expect, and let the others stop the program. The ones that you do not expect are bugs, and bugs should be loud.
except:. It hides bugs.try. Put only the risky line, so that you know which line failed.except part that does nothing (pass). The error vanishes without a trace.except ValueError does not catch a ZeroDivisionError.try in the except when the line failed before the variable was made.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
try the risky lines. except a named error. else when fine. finally always.except parts, or a tuple of kinds, and as error for the error itself.try.except:.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
else run?finally run?except: with no name risky?int("abc") give, and which does int(None) give?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.