Syntax errors and exceptions, the built-in exceptions, raise and assert, and the order in which try, except, else and finally run.
| ZeroDivisionError | dividing by zero with /, // or % |
| ValueError | right type, wrong value: int('ten'); L.remove(x) when x is not in L |
| TypeError | the wrong type: '5' + 3; len(42); changing a tuple |
| IndexError, KeyError, NameError | an index out of range; a missing dictionary key; a name never given a value |
| EOFError, IOError, ImportError | reading past the end (pickle.load); a file problem; a missing module |
| The order | no exception: try, else, finally; an exception: try up to it, except, finally |
Write the letters this prints, in order, separated by commas.
try:
x = 2
y = x / 0
print('A')
except ZeroDivisionError:
print('B')
else:
print('E')
finally:
print('C')
print('D')
Working: x / 0 raises ZeroDivisionError, so A and else are skipped; finally always runs; then the program carries on
Answer: B, C, D