Exception Handler on a Switch I am working on rewriting an application in Laravel 5.1. I am new to the exception handling technique introduced in 5.0. I have overlooked taking advantage of throwing/catching exceptions frequently in the past, but am working on this now. I am most likely not up to speed on some of the best practices.



public function render($request, Exception $e)
{

    switch (true) {

        case $e instanceof Exceptions\Auctions\AuctionTypeException:
            // Should I throw a new exception here? Is passing the old exception message to a new exception good practice?
            throw new Exceptions\Notifications\AlertException($e-&gt;getMessage());
            break;

        case $e instanceof Exceptions\Auctions\AuctionArgumentException:
            // Should I throw a new exception here? Is passing the old exception message to a new exception good practice?
            throw new Exceptions\Notifications\AlertException($e-&gt;getMessage());
            break;

        ////////////////////////////////////////////
        // Custom errors to display in error view //
        ////////////////////////////////////////////
        case $e instanceof Exceptions\Notifications\AlertException:

            return response()-&gt;view('errors.notification', ['message' =&gt; $e-&gt;getMessage(), 500]);
            break;

        //////////////////////////////////////////////
        // Unknown exceptions are rendered normally //
        //////////////////////////////////////////////
        default:
            return parent::render($request, $e);
    }

}


Docs for the exception handler are here.

While most of my exceptions will fall under the "explicitly ignore and continue" umbrella, I feel like I will need to handle my exceptions in a more abstract/polymophic way later down the road, so I am preparing now. This is a huge app, and I will have several custom Exceptions.

This code currently works and behaves as excepted. Exceptions that I explicitly state I want to do something special with can be added as a case, and if it is new, or I just want to handle it normally...it falls under the default.

I would like to know if I am going down the right path - to me, this seems like a fairly clever way to handle this. Is it a good idea to catch an exception, only to throw another exception with the previous exceptions message? Are there any pitfalls to checking the instance and evaluating true like this?
There is nothing wrong with rethrowing an exception of a different type, the problem lies with how you are doing it. According to the PHP docs, you can pass a previous exception as a second argument to the new exception. This preserves the original stack trace. I would also provide a new error message as well:

throw new MyException("A new error summary", $e);


A new take on your switch statement:

public function render($request, Exception $e)
{

    switch (true) {

        case $e instanceof Exceptions\Auctions\AuctionTypeException:
            throw new Exceptions\Notifications\AlertException("An auction type failed to save", $e);
            break;

        case $e instanceof Exceptions\Auctions\AuctionArgumentException:
            throw new Exceptions\Notifications\AlertException("An auction argument was not valid", $e);
            break;

        ////////////////////////////////////////////
        // Custom errors to display in error view //
        ////////////////////////////////////////////
        case $e instanceof Exceptions\Notifications\AlertException:

            return response()-&gt;view('errors.notification', ['message' =&gt; $e-&gt;getMessage(), 500]);
            break;

        //////////////////////////////////////////////
        // Unknown exceptions are rendered normally //
        //////////////////////////////////////////////
        default:
            return parent::render($request, $e);
    }

}