| int(text) on '7.5' | ValueError | catch at the input loop, ask again |
| Example: int('7.5') fails on the dot: float('7.5') gives 7.5 and int(7.5) gives 7, but int reads digits only. |
| fields[1] on ['Feb'] | IndexError | catch in the reading loop, count and skip |
| Example: 'Feb'.split(',') has 1 element, index 0 only; 'Feb,'.split(',') has 2, and fields[1] is the empty string ''. |
| open(name, 'r'), no such file | FileNotFoundError | catch where the name came from |
| Example: A name typed by the user is asked again; a name fixed in the code climbs, and the traceback names the file and the line, 1 open to fix. |
| 'total: ' + 12 | TypeError | let it climb: a bug of yours |
| Example: 'total: ' + str(12) is 'total: 12', 9 characters; the version without str never runs, and no handler should hide that. |
| total / count, count is 0 | ZeroDivisionError | guard the count, never catch |
| Example: 0 records read: the mean does not exist, so return None or print 'no data', and never 0, which is a mean of something. |
| except: | everything, Ctrl-C included | safe because it catches all rule that does not exist |
| Example: In a loop that asks 100 times, the interrupt key is swallowed 100 times, and a NameError typed in the handler is reported as 'invalid input' for ever. What to do: Name the class: except ValueError:, or at the very widest except Exception:, which lets KeyboardInterrupt and SystemExit through. |