Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
PYTHON FUNCTIONS / CHAPTER 04 / Learn / LESSON 06
Sixth class. Some errors are caught with try/except. Catch specific types — ValueError, TypeError — not a bare except:.
def parse_int(text): try: return int(text) except ValueError: return None
int("x") raises ValueError. You catch it and return None instead of crashing.
TypeError comes from incompatible types — for example "a" * "b". Note: 3 * "x" does not raise (you get "xxx"). Today we practise both.
Common mistake: bare except: — hides bugs; or wrapping too much code in one try.
Learn: catch specific exception types.
AGGREGATE IDLE
›