I have this very simple PHP 8 code in which there is (voluntary) an error. Just to see if the exception is caught.
try {
$school = $this->schoolRepository->insert($request->all());
$fake = 1 / 0;
return new SchoolResource($school);
} catch (\Exception $e) {
return response()->json('bad request' . $ex->getMessage(), 400);
}
And surprise ! The exception is not caught. I receive the error of the division :
{
"message": "Division by zero",
"exception": "DivisionByZeroError",
"file": "D:\\Workspace\\school-back-v2\\app\\Http\\Controllers\\SchoolController.php",
"line": 48,
"trace": [
Why is this exception not caught ?
CodePudding user response:
DivisionByZeroError inherits from Error, so you'd better catch the base interface Throwable.
catch (\Throwable $e)
CodePudding user response:
change the return response()->json('bad request' . $ex->getMessage(), 400); to return response()->json('bad request' . $e->getMessage(), 400);
