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

Amrith's avatar

User::findOrFail() exception control without TRY CATCH Block

Im trying to catch this error without try catch block. but it doesn't work . please help me to fix this .

RegisterController.php

   $isUserCreate = User::findorFail(30);
 throw_if(!$isUserCreate, Exception::class, 'test', 404);

Handler.php

 public function register()
    {

        $this->renderable(function (Exception $e,$request) {

            return Response::json([
                'message' => $e->getMessage(),
                'code' => $e->getCode(),
            ], $e->getCode());
        });


    }

Output

POST http://127.0.0.1:8000/api/auth/register

HTTP/1.1 404 Not Found
Host: 127.0.0.1:8000
Date: Fri, 04 Feb 2022 20:24:25 GMT
Connection: close
X-Powered-By: PHP/8.0.13
Cache-Control: no-cache, private
Date: Fri, 04 Feb 2022 20:24:25 GMT
Content-Type: application/json
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 58
Access-Control-Allow-Origin: *

{
  "message": "No query results for model [App\Models\User] 30",
  "exception": "Symfony\Component\HttpKernel\Exception\NotFoundHttpException",
  "file": "D:\Project Repo\Lectures\Backend\PHP REST API\laravelapidemo\vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php",
  "line": 385,
  "trace": [
    {
    
    {
0 likes
6 replies
tykus's avatar

That's how you catch Exceptions (with try/catch)...

If you don't want the Exception, what do you want?

public function render($request, Throwable $e)
{
    if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
        return Response::json([
            'message' => $e->getMessage(),
            'code' => $e->getCode(),
        ], $e->getCode());
    }

    return parent::render($request, $e);
}
Amrith's avatar

@tykus No,I want to tackle this exception without using try catch block.


   $isUserCreate = User::findorFail(30);


        throw_if(!$isUserCreate, Exception::class, 'test', 404);

tykus's avatar
tykus
Best Answer
Level 104

@Amrith you can handle the ModelNotFoundException in the app/Exceptions/Handler.php by implementing the render method like I have shown above.

Or, you don't use the findOrFail method:

$isUserCreate = User::find(30);
throw_unless($isUserCreate, // ...)
1 like
Amrith's avatar

@tykus Thanks mate. here is my edited code , according to this code. how can i catch another exception. I want to handle all exception in global way. without using controller's try() catch{}

@tykus ,What is different beween renderable method vs render method

RegisterationController.php

 $isUserCreate = User::findorFail(30);

handler.php

 public function register()
    {

        $this->renderable(function (Exception $e, $request) {


            if ($request->wantsJson()) {
                return Response::json([
                    'myMessage' => $e->getMessage()
                ], $e->getStatusCode());
            }
        }

        );


    }

tykus's avatar

@Amrith there is no difference between the renderable Closure and the render method - both will allow you to define your own responses either for your own custom exceptions, or to intercept before the default render methods of built-in Exception classes.

If you're all set; please mark your thread(s) closed.

1 like

Please or to participate in this conversation.