I found a different behaviour when I run the following code in Laravel and in a simple php script.
try {
$a=null; $a[3];
var_dump('ok');
} catch (\Exception $e) {
var_dump('error');
}
In Laravel it returns error
but in the simple php script it returns ok
.
I was wonder how can I set to return error
in php script also.
CodePudding user response:
This happens because PHP classifies "accessing an array offset on type null" as a warning and not as an exception. Since you only try to catch exceptions and are given warnings, it will never enter the catch block. Since under the hood Laravel translates warnings into exceptions, the catch block will be entered in Laravel and therefore output error.
This answer will explain how to catch warnings.
Pretty sure you can also try your hand at editing the register()
method in Handler.php
to create a custom error handler, but there are probably plenty of guides online.