# Correct exception handling with openrouteservice.exceptions

**URL:** https://ask.openrouteservice.org/t/correct-exception-handling-with-openrouteservice-exceptions/1734
**Category:** python
**Created:** [May 24, 2020, 10:21am UTC](https://ask.openrouteservice.org/t/correct-exception-handling-with-openrouteservice-exceptions/1734 "2020-05-24T10:21:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![aarondlc](https://ask.openrouteservice.org/user_avatar/ask.openrouteservice.org/aarondlc/32/451_2.png) [@aarondlc](https://ask.openrouteservice.org/u/aarondlc)
#### Post date: [May 24, 2020, 10:21am UTC](https://ask.openrouteservice.org/t/correct-exception-handling-with-openrouteservice-exceptions/1734/1 "2020-05-24T10:21:06Z")

</div>

Good morning,  
I’m trying to create a try/except block to catch timeout and api errors, but It doesnt seem to recognize the errors, for example:

```
try:

  api_key = 'erersfsdfdsfdsfs'

except openrouteservice.exceptions.ApiError:
   slck.send('API out of tokens, refresh)

```

Which would send an slack message on failure, same logic with timeout. Can someone point my error?  
Thanks

---

<div class="post-metadata">

### Author: ![nils](https://ask.openrouteservice.org/user_avatar/ask.openrouteservice.org/nils/32/6_2.png) [@nils](https://ask.openrouteservice.org/u/nils)
#### Post date: [May 24, 2020, 10:30am UTC](https://ask.openrouteservice.org/t/correct-exception-handling-with-openrouteservice-exceptions/1734/2 "2020-05-24T10:30:13Z")

</div>

What should throw an error there? It’s just an assignment…

If you would actually do a request with that API key to our servers, you’d get a `403 - Unauthorized` and trigger a `ApiError`.

---

<div class="post-metadata">

### Author: ![aarondlc](https://ask.openrouteservice.org/user_avatar/ask.openrouteservice.org/aarondlc/32/451_2.png) [@aarondlc](https://ask.openrouteservice.org/u/aarondlc)
#### Post date: [May 24, 2020, 11:06am UTC](https://ask.openrouteservice.org/t/correct-exception-handling-with-openrouteservice-exceptions/1734/3 "2020-05-24T11:06:08Z")

</div>

Yes, this is part of a larger script and I throws the expected 403, the problem is that it doesnt send the message. I tried a simpler "print something) and didnt either, so the problem is in how the error is catched isnt it?

---

<div class="post-metadata">

### Author: ![amandus](https://ask.openrouteservice.org/user_avatar/ask.openrouteservice.org/amandus/32/669_2.png) [@amandus](https://ask.openrouteservice.org/u/amandus)
#### Post date: [May 24, 2020, 2:08pm UTC](https://ask.openrouteservice.org/t/correct-exception-handling-with-openrouteservice-exceptions/1734/4 "2020-05-24T14:08:53Z")

</div>

In general id would be helpful if you actually include the part that would throw the error.  
As nils mentioned, the assignment will never throw an error.

You could check for general Exception and inspect if it really is

```auto
openrouteservice.exceptions.ApiError

```

you are looking for.

But again, without context it’s hard to say what your problem is.
