Key Ideas
1What Are Exceptions?. An exception is a signal that something went wrong at runtime. When Python hits an error it cannot handle (dividing by zero, missing file, wrong type), it raises an ex...
2try / except / else / finally. The full try block has four clauses. You don't need all of them every time — but understanding each one is essential for writing production-quality code.
3Raising Exceptions & Custom Exceptions. You can raise exceptions yourself with the raise keyword — when input is invalid, a business rule is violated, or a state is impossible. Creating custom exception clas...
4Context Managers & __enter__ / __exit__. The with statement you've been using for files is powered by the context manager protocol. Any class implementing __enter__ and __exit__ can be used with with. __exit_...