Why does Python abandon the rest of a try block the instant a line raises — and where exactly does execution land?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Each of these snippets raises an exception the moment it runs:
nums = [10, 20, 30], then nums[7]'total: ' + 5int('hello')print(subtotal), where subtotal was never definedAssign to exception_names a list of four strings naming the exception each snippet raises, in that same order. Spell each name the way Python spells it (the way KeyboardInterrupt is spelled — capitals and no spaces).
Exceptions as named error types
List IndexError, TypeError, SyntaxError, NameError and ValueError with the situation that raises each, and record that a raised exception need not end the program.
Control flow of try and except
Write the rule that a try block finishing cleanly skips the except entirely, while a raise abandons the remaining try lines and jumps straight into the except block.
The if/else analogy for the construct
Record the mapping of try onto "run these lines" and except onto "here is what to do if they would have crashed", noting the analogy is not code to write.
sum_digits with and without handling
Write both versions, the one guarded by if c in '0123456789' and the one using try: total += int(c), and trace sum_digits('123abc') character by character to the returned 6.