PYTHON INTERMEDIATEChapter 7 · Errors, Files and Modules
Exceptions
Use try and except blocks to handle exceptions. This prevents your program from crashing when a runtime error occurs.
Worked example
How it reads
- try: wraps the code that might fail at runtime
- except ValueError: catches and handles specific ValueErrors

Cloud tip: Always catch specific errors (like ValueError or KeyError) instead of a generic Exception.


