While Python provides a wide variety of built-in exceptions, sometimes there’s a need to define custom exceptions tailored for specific applications or libraries. Custom exceptions provide a way to signal specific error conditions that are unique to your program’s domain.
To create a custom exception, you define a new class derived from Python’s built-in BaseException
class or one of its derived classes (typically Exception
).
Example:
class AppError(Exception):
"""Base exception for the application."""
class ConfigurationError(AppError):
"""Raised when there's a configuration error."""
class DatabaseError(AppError):
"""Raised when a database operation fails."""
You might often want to include additional information within your exceptions. This can be achieved by extending the __init__
method.
Example:
class ValidationError(Exception):
def __init__(self, message, field):
super().__init__(message)
self.field = field
error = ValidationError("Invalid value", "age")
print(error.field) # Output: age
In situations where you want to raise a custom exception in response to catching another exception, you can use exception chaining to maintain the context of the original exception.
Example:
class ParsingError(Exception):
pass
try:
int("not a number")
except ValueError as e:
raise ParsingError("Failed parsing input") from e
In this example, if you catch and inspect ParsingError
, you can also access the original ValueError
.
Just as with standard exceptions, you can use custom exceptions with the assert
statement to validate assumptions:
class AgeNegativeError(Exception):
pass
age = -5
assert age >= 0, AgeNegativeError("Age cannot be negative")
Custom exception handling is a crucial aspect of crafting a robust and maintainable Python application. It allows developers to define and communicate specific error conditions cleanly and effectively. By understanding how to define and use custom exceptions, developers can build more fault-tolerant systems and improve the user experience by providing meaningful error messages.