3

I have code in RouteServiceProvider:

$router->bind('user', function ($value) {
    try{
        throw (new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
    }catch(Exception $e){
        exit('nott');
    }
});

and I don not geting output

nott

I am getting

Sorry, the page you are looking for could not be found.
NotFoundHttpException in RouteServiceProvider.php line 75:
...

EDITED: This works:

$router->bind('user', function ($value) {
    try{
        throw (new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
    }catch(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e){
        exit('addd');
    }
});

But this not works:

$router->bind('user', function ($value) {
    try{
        return (new User)->findOrFail(122);
    }catch(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e){
        exit('addd');
    }
});
2
  • Off topic, but in general it's considered bad practice to catch the general Exception class; you should instead do a catch on the specific type(s) of exception that you anticipate getting. (btw: the practice of catching the general Exception class is jokingly called "Pokemon exception handling" -- because you're gonna catch 'em all) Commented Feb 23, 2016 at 8:58
  • The problem you're getting is not related to Laravel, it's related to Namespacing. When you use namespaces in a PHP class, you must then reference the full namespace of all classes it uses. Even plain PHP ones in the top-level namespace need a backslash to reference their namespace. Or you can tell the class to use them, and then you won't need the namespace. (the same applies to the Symphony exception class you're using, and to any other class; you can have a use statement for them at the top of your class, then you don't need to give the full namespace in your code body. Much neater. Commented Feb 23, 2016 at 9:03

1 Answer 1

9
$router->bind('user', function ($value) {
    try{
        throw (new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
    }catch(\Exception $e){
        exit('nott');
    }
});

OR

use Exception; //on top

    $router->bind('user', function ($value) {
        try{
            throw (new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
        }catch(Exception $e){
            exit('nott');
        }
    });

i think now you understand what you missing.

Sign up to request clarification or add additional context in comments.

2 Comments

OP probably has these codes in the routes.php file of Laravel which doesn't have any namespace expression, thus the use Exception; part is unnecessary.
try to catch(\Illuminate\Database\Eloquent\ModelNotFoundException $e) because findorfail throw model not found exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.