Predict the Exception Hierarchy
Beginner · Predict the output · from Python Custom Exceptions: Build Your Own Error Types
From the lesson: Python Custom Exceptions: Build Your Own Error Types
Imagine your car's dashboard had only one warning light that just said "Problem." Not very helpful, right? You'd want to know: is the engine overheating? Is the oil low? Is a door open? The more specific the warning, the faster you can fix the issue.
Read the lessonRead the code below and predict what it will print.
class AppError(Exception):
pass
class DatabaseError(AppError):
pass
class ConnectionError(DatabaseError):
pass
errors = [ConnectionError("timeout"), DatabaseError("corrupt"), AppError("unknown")]
for e in errors:
try:
raise e
except ConnectionError:
print("connection")
except DatabaseError:
print("database")
except AppError:
print("app")# Type the exact output using print statements
More Custom Exceptions exercises
- Create a Custom ExceptionBeginner
- Exception with Extra DataIntermediate
- Build a Shopping Cart Exception HierarchyAdvanced
- Refactor: Replace Generic with Custom ExceptionsAdvanced
All Python exercises · 50 practice problems · Error Handling quiz