try and except
Put code that might fail in a try block. If it raises, Python jumps to the
first except clause whose type matches. except KeyError as e also gives
you the exception object, and str(e) is its message.
Catch the specific error you expect. A tuple, except (TypeError, ValueError), catches several. Keep the try block small, so you don't catch
errors from lines you didn't mean to guard.
Never write a bare except:. It catches everything, including typos:
try:
total = sum(prces) # NameError: typo
except:
total = 0 # the typo is hidden, total is silently wrong
A bare except even catches Ctrl+C. except Exception is a little better, but
still hides bugs. Interviewers notice both.
Run it, then add lookup(prices, None). Which clause catches it?