# Обработка ошибок

Есть два способа обработки ошибок API.

## Способ 1

Использование конструкции try catch

```php
$api = new VkApi('access_token', 5.131, ignore_error: false); // выключаем игнор. ошибок, дабы при их наличии выбрасывалось исключение

try {
    $api->messages->send() // будет получена ошибка из-за недостаточного кол-ва аргументов
} catch (VkApiError $error) {
    $error_code = $error->getCode();
    $error_msg = $error->getMessage();
}
```

{% hint style="info" %}
Рекомендуется использовать этот способ. Так вы точно не пропустите ни одну ошибку.
{% endhint %}

{% hint style="danger" %}
Учитывайте, что все методы-утилиты, которые являются лишь обреткой над API-методами - также будут выбрасывать исключения.
{% endhint %}

## Способ 2

Ошибки API не будут выбрасывать исключения, но в объекте ответа будут присутстсовать поле error.

```php
$api = new VkApi('access_token', 5.131, ignore_error: true); // по умолчанию и так true

$request = $api->messages->send() // будет получена ошибка из-за недостаточного кол-ва аргументов
if (isset($request->error)) {
    $error_code = $request->error->error_code;
    $error_msg = $request->error->error_msg;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vkfast.gitbook.io/docs/other/error-handle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
