Module 14: Exceptions, context managers and testing
Custom exceptions, writing your own with-blocks, logging and testing with pytest.
- Advanced exceptions: Subclass Exception for your own errors (with a base class), chain with raise New(...) from e, and never return inside finally.
- Context managers: writing your own `with`: with calls enter (its return is the as name) and always exit; a truthy exit suppresses the error; with @contextmanager, wrap yield in try/finally.
- Logging instead of print: Log with levels (DEBUG < INFO < WARNING < ERROR < CRITICAL, default WARNING), set them up once with basicConfig, and raise when the caller must act.
- Testing: asserts, pytest, unittest and mocks: A test calls your code and asserts the result; pytest finds test functions (fixtures, parametrize, pytest.raises); unittest.mock fakes what you cannot control.
Ends with a 12-question checkpoint (80% passes) and a mini project: Canteen Counter, robust edition.