Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jordan.dobrev.88@gmail.com's avatar

How to handle Laravel 5 Exceptions

Hi,

I am trying to find a way to handle exceptions in the way I did in Laravel 4.2, but I can't find the proper way to do it. In Laravel 4.2 I used them for different Error handling, custom 404 page and logging fatal errors or sending emails to the administrator. The case with Laravel5 seems to be different. I've read a lot of topics and most people say they used ErrorServiceProvider that comes with Laravel 5 but Tylor decided to remove it from the project. I see there is a Handler class in the Exception folder but it does not feel as intuitive to put it there as it was with App:error, App::fatal and App::missing in the global.php file in Laravel 4. How do I achieve my goal and keep it simple?

0 likes
12 replies
laloutre's avatar

Not sure how to exactly, but I heard Taylor saying the new way will be to use regular try... catch in the Kernels classes.

JohnRivs's avatar

You can either use a Service Provider, or if you feel comfortable with the idea of global.php, just load a file via composer.json and put the App::error, App::fatal and all those right there.

jordan.dobrev.88@gmail.com's avatar

I saw the Kernel class handle method try/catch method but it seems a little bit messy and it looks like you are doing something in a place you should leave as simple as possible. Service provider is a good idea but how exactly do you achieve it with it?

bestmomo's avatar
Level 52

There is the app/Exceptions/handler.php to manage exceptions. I send 404 error there :

public function render($request, Exception $e)
{
    if ($this->isHttpException($e))
    {       
        if($e instanceof NotFoundHttpException)
        {
            return response()->view('front.missing', [], 404);
        }
        return $this->renderHttpException($e);
    }
    return parent::render($request, $e);
}
6 likes
rapliandras's avatar

Based on bestmomo's answer, that does not work for my dev version, but this does:

    public function render($request, Exception $e)
    {
        if($e->getStatusCode()===404)
        {
            return response()->view('errors.404', [], 404);
        }
        return parent::render($request, $e);
    }
bestmomo's avatar

May be will work with :

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2 likes
rogierv's avatar

I took the parent Illuminate\Foundation\Exceptions\Handler isHttpException function and copied it to app/Exceptions/Handler.php and changed it's name to my isUserNotFoundException.

    protected function isUserNotFoundException(Exception $e)
    {
        return $e instanceof UserNotFoundException;
    }

and than in the render function add

        if ($this->isUserNotFoundException($e))
            return redirect()->home()->with('notification', $e->getMessage() );
jordan.dobrev.88@gmail.com's avatar

Yes it works like that but am I the only one that thinks this is far more messy than the Laravel 4 approach? Tylor actually spent some time in the past thinking about exceptions, errors and 404 rendering and out of the sudden it's all gone. I know it's still in development but I really hope they are gonna come up with sth simple and nice - the way they do it best.

TKay's avatar

Hi,

I'm getting PDOException which needs to be handled by Exception. In app/Exceptions/Handler.php I wrote inside render()

    if ($e instanceof PDOException) {
                return response()->view('errors.404', [], 404);
    }

But still get the exception extracting whole detail "Whoops, looks like something went wrong."

Is there anything i need to configure to use this exception handler? My laravel version 5.2

stuartcusack's avatar

on Laravel 5.3 I had to use bestmomo's answer along with name spacing, like so:

if($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
//do whatever 
}
2 likes
the94air's avatar

If you forget using the exception in the handler.php

use \Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Be careful, no errors will show if you forget to add the name space. But the exception will not work.

Please or to participate in this conversation.