# Exceptions

## mt5cli.exceptions

Normalized exception types for MT5 and mt5cli operations.

### T `module-attribute`

    T = TypeVar('T')

### __all__ `module-attribute`

    __all__ = [
        "Mt5CliError",
        "Mt5ConnectionError",
        "Mt5OperationError",
        "Mt5SchemaError",
        "call_with_normalized_errors",
        "is_recoverable_mt5_error",
        "normalize_mt5_exception",
    ]

### Mt5CliError

Bases: `Exception`

Base exception for mt5cli public API errors.

### Mt5ConnectionError

Bases: `Mt5CliError`

Raised when MT5 initialization, login, or shutdown fails.

### Mt5OperationError

Bases: `Mt5CliError`

Raised when an MT5 data or trading operation fails.

### Mt5SchemaError

Bases: `Mt5CliError`

Raised when a DataFrame does not match an expected dataset schema.

### call_with_normalized_errors

    call_with_normalized_errors(fn: Callable[[], T]) -> T

Run `fn` and map recoverable MT5 errors to mt5cli types.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`fn` | `Callable[[], T]` | Callable performing MT5 work. | required

Returns:

Type | Description
--- | ---
`T` | Value returned by `fn`.

Source code in `mt5cli/exceptions.py`

    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    def call_with_normalized_errors(fn: Callable[[], T]) -> T:
        """Run ``fn`` and map recoverable MT5 errors to mt5cli types.

        Args:
            fn: Callable performing MT5 work.

        Returns:
            Value returned by ``fn``.
        """
        try:
            return fn()
        except _RECOVERABLE_MT5_ERRORS as exc:
            normalized = normalize_mt5_exception(exc)
            raise normalized from exc

### is_recoverable_mt5_error

    is_recoverable_mt5_error(exc: BaseException) -> bool

Return whether an exception is a transient MT5 failure worth retrying.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`exc` | `BaseException` | Exception raised by MT5 or pdmt5. | required

Returns:

Type | Description
--- | ---
`bool` | True for `Mt5RuntimeError` and `Mt5TradingError` (if available).

Source code in `mt5cli/exceptions.py`

    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    def is_recoverable_mt5_error(exc: BaseException) -> bool:
        """Return whether an exception is a transient MT5 failure worth retrying.

        Args:
            exc: Exception raised by MT5 or pdmt5.

        Returns:
            True for ``Mt5RuntimeError`` and ``Mt5TradingError`` (if available).
        """
        return isinstance(exc, _RECOVERABLE_MT5_ERRORS)

### normalize_mt5_exception

    normalize_mt5_exception(exc: BaseException) -> Mt5CliError

Map pdmt5/MT5 exceptions to stable mt5cli exception types.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`exc` | `BaseException` | Original exception from MT5 or pdmt5. | required

Returns:

Type | Description
--- | ---
`Mt5CliError` | `Mt5ConnectionError` for runtime failures, `Mt5OperationError` for
`Mt5CliError` | trading failures, or the original exception when it is not recognized.

Source code in `mt5cli/exceptions.py`

    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    def normalize_mt5_exception(exc: BaseException) -> Mt5CliError:
        """Map pdmt5/MT5 exceptions to stable mt5cli exception types.

        Args:
            exc: Original exception from MT5 or pdmt5.

        Returns:
            ``Mt5ConnectionError`` for runtime failures, ``Mt5OperationError`` for
            trading failures, or the original exception when it is not recognized.
        """
        if Mt5TradingError is not None and isinstance(exc, Mt5TradingError):
            return Mt5OperationError(str(exc))
        if isinstance(exc, Mt5RuntimeError):
            return Mt5ConnectionError(str(exc))
        if isinstance(exc, Mt5CliError):
            return exc
        return Mt5CliError(str(exc))

#### Search

From here you can search these documents. Enter your search terms below.

#### Keyboard Shortcuts

Keys | Action
--- | ---
`?` | Open this help
`n` | Next page
`p` | Previous page
`s` | Search