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

DcDev's avatar
Level 1

Custom Exception doesnt work

Maybe I'm doing it wrong, but it doesnt't work:

This is my custom Exception:

class RolesSearchNotFoundException extends Exception
{
    /**
     * Report the exception.
     *
     * @return bool|null
     */
    public function report()
    {
        Log::warning('Hooray'); // doesnt work
    }

    /**
     * Render the exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function render($request)
    {

    }
}

This is Handler.php:

public function register()
    {
        $this->reportable(function (RolesSearchNotFoundException $e) {
            dd($e); // this doesnt work
        });

        $this->renderable(function (\Exception $e) {
            if ($e->getPrevious() instanceof \Illuminate\Session\TokenMismatchException) {
                return redirect()->back()
                    ->withInput(request()->except('_token'))
                    ->withErrors(['errors' => __('app.errors.invalid_token')]);
            };
        });
    }

And now I'm throwing my custom exception:

public function getRoleOptions(): array
    {
        $roles = $this->roleService->searchRoles();
        $rolesOptions[''] = '';
        $roles = []; // I'm making it empty for this test

        if(count($roles) == 0)
            throw new RolesSearchNotFoundException('Roles not found for form options');

        return $roles;
    }

Catching:

try {
            return $this->getRoleOptions();
        } catch (RolesSearchNotFoundException $exception) {
						abort(500, 'Roles not found');
        }

Aborting works - so it catches RolesSearchNotFoundException.

Reportable in register function doesnt receive RolesSearchNotFoundException in callback, but if I change callback argument to Throwable, dd() shows native PHP exception:

ErrorException {#1704 ▼
  #message: "Undefined variable $options"
  #code: 0
  #file: "/app/Services/User/Retrievers/UserGetSelectOptionsWithKeyRetriever.php"
  #line: 22
  #severity: E_WARNING
  trace: {▶}
}

Neither report() method in RolesSearchNotFoundException Logs my message.

0 likes
4 replies
DcDev's avatar
Level 1

I think I didnt get it right. I removed try/catch block aroud return $this->getRoleOptions(); and report() worked and I realised that one of 2 variants works:

  1. I use try/catch and make reporting/rendering inside catch block;
  2. I dont use try/catch block (just return $this->getRoleOptions();) and make reporting/rendering inside custom exception class or Handler.php;

Am I right? If it is so, for me #1 is better, because it makes code more readable and I see what is going on in this place. #2 is sileте way of reporting something less important.

What do you think of it, what strategy is right?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Yes that is correct. It was what I was pointing to.

If you always want to return the same stuff for that exception, then don't try/catch and let laravel handle it and set the logic in the exception or Handler.php

If you need specific data returned for this route or allow the code continue to run, you should do #1

Please or to participate in this conversation.