> For the complete documentation index, see [llms.txt](https://doc.bunq.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.bunq.com/getting-started/tools/unmaintained-software-development-kits-sdks/python/exceptions.md).

# Exceptions

When you make a request via the SDK, there is a chance of request failing due to various reasons. When such a failure happens, an exception corresponding to the error occurred is raised.

***

**Possible Exceptions**

* `BadRequestException` If the request returns with status code `400`
* `UnauthorizedException` If the request returns with status code `401`
* `ForbiddenException` If the request returns with status code `403`
* `NotFoundException` If the request returns with status code `404`
* `MethodNotAllowedException` If the request returns with status code `405`
* `TooManyRequestsException` If the request returns with status code `429`
* `PleaseContactBunqException` If the request returns with status code `500`. If you get this exception, please contact us preferably via the support chat in the bunq app.
* `UnknownApiErrorException` If none of the above mentioned exceptions are raised, this exception will be raised instead.

For more information regarding these errors, please take a look on the documentation page here:&#x20;

{% content-ref url="/pages/H5Lac42NYyIDvkQiW8y2" %}
[Errors](/basics/errors.md)
{% endcontent-ref %}

***

### **Base exception**

All the exceptions have the same base exception which looks like this:

```python
class ApiException(Exception):
    def __init__(self,
                 message: str,
                 response_code: int) -> None:
        pass

    @property
    def message(self) -> str:
        return self._message

    @property
    def response_code(self) -> int:
        return self._response_code
```

This means that each exception will have the response code and the error message related to the specific exception that has been raised.

***

### **Exception handling**

Because we raise different exceptions for each error, you can catch an error if you expect it to be raised.

```python
from bunq.sdk.exception.bad_request_exception import BadRequestException
from bunq.sdk.context.api_context import ApiEnvironmentType, ApiContext

API_KEY = "Some invalid API key"
DESCRIPTION = "This will raise a BadRequestException"

try:
    # Make a call that might raise an exception
    ApiContext.create(ApiEnvironmentType.SANDBOX, API_KEY, DESCRIPTION)
except BadRequestException as error:
    # Do something if exception is raised
    print(error.response_code)
    print(error.message) # or just print(error)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://doc.bunq.com/getting-started/tools/unmaintained-software-development-kits-sdks/python/exceptions.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
