"""Create error_monitor
Generated by Phi-Octopus Eden
2025-11-07 17:43:04.128160
"""

```python
import sys
from typing import Callable, Any


class ErrorMonitor:
    """
    A class responsible for monitoring errors during program execution.
    It allows defining custom error handlers to handle specific exceptions.

    Attributes:
        handlers: A dictionary mapping exception types to their corresponding handler functions.
    
    Methods:
        add_handler: Register a new error handler function.
        monitor: Run the monitored code with an error handler, allowing limited recovery from errors.
    """

    def __init__(self):
        self.handlers = {}

    def add_handler(self, exc_type: type[BaseException], handler: Callable[[Any], Any]) -> None:
        """
        Register a new error handler function for a given exception.

        Args:
            exc_type: The type of the exception to handle.
            handler: A callable that takes an exception instance and returns a recovery action or result.
        
        Raises:
            ValueError: If `exc_type` is not derived from BaseException.
        """
        if not issubclass(exc_type, BaseException):
            raise ValueError("exc_type must be a subclass of BaseException")
        self.handlers[exc_type] = handler

    def monitor(self, code: Callable[..., Any]) -> Callable[..., Any]:
        """
        Run the monitored code with an error handler.

        Args:
            code: A callable that represents the code to execute under monitoring.
        
        Returns:
            A wrapper function that handles exceptions and attempts recovery.
        """

        def wrapper(*args, **kwargs) -> Any:
            try:
                return code(*args, **kwargs)
            except Exception as exc:
                handler = self.handlers.get(type(exc))
                if handler is not None:
                    result = handler(exc)
                    # Re-raise the exception if no recovery action was returned
                    if result is None:
                        raise
                    return result

        return wrapper


def example_handler(exc: BaseException) -> Any:
    """
    An example custom error handler function.

    Args:
        exc: The exception instance that occurred.
    
    Returns:
        A simple string indicating the recovery action was taken or None if no recovery is possible.
    """
    print(f"Error caught: {exc}")
    return "Recovery action taken"


# Example usage
if __name__ == "__main__":
    monitor = ErrorMonitor()
    monitor.add_handler(ValueError, example_handler)

    def risky_function() -> int:
        """This function may raise a ValueError."""
        if 'error' in sys.argv[1:]:
            raise ValueError("Simulated error")
        return 42

    safe_function = monitor.monitor(risky_function)
    
    result = safe_function()
    print(f"Result: {result}")
```

```python
# Run example code with an argument to trigger the error
# python error_monitor_example.py --error
```
```python
# Run example code without triggering the error
# python error_monitor_example.py
```