# Slim 4 - Error throw HttpNotFoundException

**URL:** <https://discourse.slimframework.com/t/slim-4-error-throw-httpnotfoundexception/3641>\
**Category:** Questions\
**Created:** [November 12, 2019, 7:28pm UTC](https://discourse.slimframework.com/t/slim-4-error-throw-httpnotfoundexception/3641 "2019-11-12T19:28:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unnamed123](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@unnamed123](https://discourse.slimframework.com/u/unnamed123)\
**Post date:** [November 12, 2019, 7:28pm UTC](https://discourse.slimframework.com/t/slim-4-error-throw-httpnotfoundexception/3641/1 "2019-11-12T19:28:34Z")

</div>

I set a custom error handler, but I have problem. When I open URL which is not support, app throw error which Uncaught:

> **Fatal error** : Uncaught Slim\Exception\HttpNotFoundException: Not found.

Why? App working if add: try{}catch(){}; But it wrongs method…

---

<div class="post-metadata">

**Author:** ![Mikalay](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/mikalay/32/720_2.png) [@Mikalay](https://discourse.slimframework.com/u/Mikalay)\
**Post date:** [November 13, 2019, 8:30am UTC](https://discourse.slimframework.com/t/slim-4-error-throw-httpnotfoundexception/3641/2 "2019-11-13T08:30:27Z")

</div>

Are there lines below in your code?

```php
use Slim\Exception\HttpNotFoundException;
use Slim\Interfaces\ErrorRendererInterface;
use Throwable;

```

Look at an example from [Rob Allen](https://akrabat.com/custom-error-rendering-in-slim-4/)

---

<div class="post-metadata">

**Author:** ![l0gicgate](https://avatars.discourse-cdn.com/v4/letter/l/41988e/32.png) [@l0gicgate](https://discourse.slimframework.com/u/l0gicgate)\
**Post date:** [November 13, 2019, 5:48pm UTC](https://discourse.slimframework.com/t/slim-4-error-throw-httpnotfoundexception/3641/3 "2019-11-13T17:48:05Z")

</div>

Sounds like you did not add the `ErrorMiddleware` to your app.

```php
$app->addErrorMiddleware(true, true, true);

```

---

<div class="post-metadata">

**Author:** ![unnamed123](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@unnamed123](https://discourse.slimframework.com/u/unnamed123)\
**Post date:** [November 13, 2019, 10:37pm UTC](https://discourse.slimframework.com/t/slim-4-error-throw-httpnotfoundexception/3641/4 "2019-11-13T22:37:37Z")

</div>

`self::$app->addErrorMiddleware(true, true, true)->setDefaultErrorHandler(ErrorHandler::class);`  
[ErrorHandler.php](https://pastebin.com/Unnp0AR3)

But i have error:  
" **Fatal error** : Uncaught Slim\Exception\HttpNotFoundException: Not found. in G:\GitHub.Repositories\Slim4\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php:91

---

<div class="post-metadata">

**Author:** ![unnamed123](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@unnamed123](https://discourse.slimframework.com/u/unnamed123)\
**Post date:** [November 13, 2019, 10:38pm UTC](https://discourse.slimframework.com/t/slim-4-error-throw-httpnotfoundexception/3641/5 "2019-11-13T22:38:34Z")

</div>

Yeah sure, it’s my handler error: [HandlerError.php](https://pastebin.com/Unnp0AR3)

---

<div class="post-metadata">

**Author:** ![l0gicgate](https://avatars.discourse-cdn.com/v4/letter/l/41988e/32.png) [@l0gicgate](https://discourse.slimframework.com/u/l0gicgate)\
**Post date:** [November 13, 2019, 10:59pm UTC](https://discourse.slimframework.com/t/slim-4-error-throw-httpnotfoundexception/3641/6 "2019-11-13T22:59:45Z")

</div>

Did you add the `ErrorMiddleware` as the very last middleware in your queue?

---

<div class="post-metadata">

**Author:** ![unnamed123](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@unnamed123](https://discourse.slimframework.com/u/unnamed123)\
**Post date:** [November 14, 2019, 4:50pm UTC](https://discourse.slimframework.com/t/slim-4-error-throw-httpnotfoundexception/3641/7 "2019-11-14T16:50:14Z")

</div>

Thanks.

It was:  
`self::$app->addErrorMiddleware(true, true, true)->setDefaultErrorHandler(ErrorHandler::class); self::$app->addRoutingMiddleware();`

Update:  
`self::$app->addRoutingMiddleware(); self::$app->addErrorMiddleware(true, true, true)->setDefaultErrorHandler(ErrorHandler::class);`
