class Custom(Exception):
def __init__(self, message, value):
super().__init__()
self.message = message
self.value = value
def __str__(self):
return self.message
def print(self):
print("warning Error")
print("message : ", self.message)
print("value : ", self.value)
try:
raise Custom("betzni eiina",273)
except Custom as e:
e.print()
### Understanding Custom Exception Handling in Python
This code snippet demonstrates how to define and use a custom exception class in Python. Here's a breakdown of its components and functionality:
#### Custom Exception Class
- **`class Custom(Exception):`**: Defines a new exception class named `Custom` that inherits from Python's built-in `Exception` class. Inheriting from `Exception` makes `Custom` a bona fide exception class, enabling it to be caught by `except` blocks.
- **`def __init__(self, message, value):`**: This is the initializer for the `Custom` class. It takes two parameters, `message` and `value`, in addition to `self`. `message` is intended to hold a description of the error, while `value` can hold any relevant numerical value associated with the error.
- `super().__init__()` calls the initializer of the base `Exception` class. This is important for proper exception hierarchy handling, although in this specific case, passing `message` to `super().__init__(message)` would also set the exception message to be accessible through standard exception handling mechanisms in Python.
- `self.message` and `self.value` store the custom message and value passed during the exception's creation.
- **`def __str__(self):`**: This special method defines how instances of `Custom` are converted to strings. Here, it returns the custom message, allowing `print(e)` or `str(e)` to display the error message directly.
- **`def print(self):`**: This method is a custom behavior for the `Custom` exception, printing a predefined warning message followed by the custom error message and value associated with the exception.
#### Exception Handling Usage
- **`raise Custom("betzni eiina",273)`**: This line raises an instance of the `Custom` exception with a specific message and value. Raising an exception immediately transfers control to the nearest enclosing `try` block that can handle the exception.
- **`except Custom as e:`**: This `except` block catches any `Custom` exceptions raised within the `try` block. The caught exception is bound to the variable `e`.
- **`e.print()`**: Calls the custom `print` method of the caught `Custom` exception, which prints the predefined warning message, the custom error message, and the associated value.
### Key Takeaways
- Defining custom exceptions in Python allows for more descriptive and granular error handling in applications, enabling developers to distinguish between different types of errors more easily.
- Custom exceptions can carry additional information about the error condition beyond just a message, as demonstrated by the `value` attribute in this example.
- Invoking `super().__init__()` in the initializer ensures that the custom exception class properly integrates with the Python exception hierarchy.
'개념 > 혼자 공부하는 파이썬' 카테고리의 다른 글
python) Explanation of the Flatten Functions (1) | 2024.11.04 |
---|---|
python) Memoization (0) | 2024.11.03 |
python) custom exception class (2) | 2024.11.01 |
python) Class Definitions and Inheritance (1) | 2024.10.31 |
python) Put only things that can be converted to numbers into a list (2) | 2024.10.30 |