Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception class not found in Laravel

When I use this code in Laravel framework:

throw new Exception("failed");

it throws this error:

FatalErrorException in MyController.php line 178: Class 'App\Http\Controllers\Exception' not found

Does anybody know how can I use (include) that Exception class to my controller? Where is actually it?

like image 896
stack Avatar asked Jan 01 '17 11:01

stack


People also ask

Which class is used to handle Exceptions in Laravel?

All exceptions are handled by the App\Exceptions\Handler class. This class contains a register method where you may register custom exception reporting and rendering callbacks.

How to handle Exception in Laravel api?

To do that, you can specify Route::fallback() method at the end of routes/api. php, handling all the routes that weren't matched. Route::fallback(function(){ return response()->json([ 'message' => 'Page Not Found. If error persists, contact [email protected]'], 404); });


1 Answers

Throw the exception using the following code.

throw new \Exception("failed");

Otherwise, import the class before using.

use Exception;
like image 162
shoieb0101 Avatar answered Oct 18 '22 09:10

shoieb0101