What an exception is
An exception stops the program at the line that failed
An unhandled exception ends the program with a traceback.
Some lines are valid Python and still cannot be carried out. The first line is fine, and prints start. The second asks for the integer in a b c. There is none, so Python raises an exception, a ValueError. Nothing catches it, so the program stops there. The third line never runs, and the screen shows a traceback that ends with the name of the exception.
What an exception is · Going deeper
Reading a traceback, and SyntaxError
Read from the bottom: the last line names the exception.
Built-in exceptions
Each kind of mistake raises its own exception
ValueError, TypeError, ZeroDivisionError, IndexError, KeyError, NameError.
The exam names six exceptions, and expects you to know what raises each. ValueError: the type is right, the value is unusable. TypeError: the operation does not exist for that type. ZeroDivisionError: dividing by zero. IndexError: a position past the end of a list, string or tuple. KeyError: a key that is not in the dictionary. NameError: a name that was never given a value.
Built-in exceptions · Going deeper
Near misses
Would another value of the same type work?
Check yourself
Which exception does this raise?
try and except
except catches the error and the program carries on
try the risky lines. except handles the failure.
To survive an exception, you catch it. Try marks the lines to watch. This one raises ValueError. The rest of the try block is abandoned. Python looks at the except line. It names ValueError, so it matches. The branch runs. And the program continues below, as if nothing had happened.
try and except · Going deeper
No error, and the wrong error
A branch catches only what it names.
Several except branches
Python runs the first except branch that matches
One try, several except branches, at most one runs.
Two different lines can fail here, in two different ways, so there are two branches. With five, nothing fails. It prints two point zero, and both branches are skipped. With a b c, the conversion raises ValueError, and the first branch runs. With zero, the conversion works and the division fails. That is a ZeroDivisionError, and the second branch runs.
Several except branches · Going deeper
Grouping, and the bare except
Specific first. Bare except: last.
Check yourself
What does this print?
Tracing the flow
An exception travels back through calls until it is caught
Raised in half, caught by the caller. Prints caught, end.
An error does not have to be caught where it happens. The function is stored. Inside the try block, half is called with zero. The division fails, inside the function. The function has no except, so it ends at once, and the error arrives back at the calling line. That line is inside a try, and the branch matches. It prints caught. Then end.
Tracing the flow · Going deeper
Check first, or catch after
Handle bad input. Fix bad code.
Check yourself
What does this print?
Lesson complete
You finished “Exceptions”
Answer the questions along the way to see your score here.