Block 134: Exception Handling in Scripts
Write robust scripts that handle errors gracefully.
Concepts
- try/except/finally/else patterns
- Catching specific exception types
- Custom exception classes
- Raising exceptions with raise
Code Examples
See exercise below.
Exercise
Wrap a file reading function with try/except to handle FileNotFoundError and PermissionError separately. Write a custom InvalidDataError exception and raise it in a data validation function.
Homework
Describe the difference between finally and else blocks in a try statement. Wednesday