2

The title is pretty self explaining: How can make sure that normal PHP error like trying to output an undefined variable.

echo $a; // ErrorException: Undefined variable: a

In PHP this would just be an Error, however in laravel errors get escalated to ErrorException which halt execution. How can I disable this. I tried

app/Exceptions/Handler.php

  protected $dontReport = [
        \Illuminate\Auth\AuthenticationException::class,
        \Illuminate\Auth\Access\AuthorizationException::class,
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
        \Illuminate\Session\TokenMismatchException::class,
        \Illuminate\Validation\ValidationException::class,
        \Illuminate\Exception\ErrorException::class, // added this
        \ErrorException::class, // and this
    ];

This question is related to my earlier question, but I thought of using another approach that might work better.

What I try to achieve is that just like with Errors the code does not stop executing but continues to run as well as it can.

online Thomas
  • 8,864
  • 6
  • 44
  • 85

2 Answers2

2

Laravel uses it's own custom error handler. According to the PHP manual:

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE(this type of error probably), E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.

Therefore Laravel is unable to catch and handle that sort of error.

What is happening there is that the error is converted to an ErrorException when the script halts execution and the shutdown function is called. There's not much you can do at that point.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
0

You can change what happens when an exception is thrown using Exception Handlers. This method will receive the thrown exception so you can handle it any way you want.

Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • and how do you suggest to continue running the code instead of exiting? – online Thomas Aug 10 '17 at 08:47
  • It's not possible to continue running the code that threw the exception because that could cause problems in the rest of the code. If you realy want the code to continue whatever happens, you will have to wrap all your code in `try/catch` blocks. – Jerodev Aug 10 '17 at 08:51