Cannot catch exceptions in laravel

I have the following situation:

  try {

        DB::beginTransaction();

        $task = new Task();
        $task->setTracker("");
        //thrown \Symfony\Component\Debug\Exception\FatalThrowableError


            DB::commit();

        }catch (\Exception $e){
            DB::rollBack();
            Log::error($e);
            //throw $e;
        }

I do not enter the catch area.
Any idea why?

Update

This is mistake:

[Symfony\Component\Debug\Exception\FatalThrowableError]
Type error: Argument 1 passed to App\Models\Task::setTracker() must be an instance of Carbon\Carbon, integer given, called in /var/www/app/Services/ShareLogic.php on line 60

and will not be caught

thank

+4
source share
3 answers

The capture Throwabledid the trick.
I do not know why? Is anyone doing

+5
source

It does not throw an exception because you are trying to catch \Exceptionone that Symfony\Component\Debug\Exception\FatalThrowableErrordoes not apply.

Instead, try to catch the actual exception by importing it.

use Symfony\Component\Debug\Exception\FatalThrowableError;

And then you can do it.

try {
    // 
} catch(FatalThrowableError e) {
    // 
}

Edit

, , PHP 7+ , PHP 5. .

try {
    // 
} catch(Error e) {
    // This should work
} catch(Throwable e) {
    // This should work as well
}
+1

Symfony Debug , , (php 7.1.x):

<?php

class MyUncatchableError extends Exception {}

function myExceptionHandler($e) {
    throw new MyUncatchableError('BANG: '.$e->getMessage());
}

set_exception_handler('myExceptionHandler');

$foo = true;

try {
    $foo->modify();
} catch (Exception $e) {
    echo 'nope';
} catch (MyUncatchableError $e) {
    echo 'nope2';
}

? :

: Uncaught MyUncatchableError: BANG: - modify() boolean in/in/WJErU: 6

:

  • 0 [ ]: myExceptionHandler ( ())
  • 1 {main}

    //WJErU 6

, .. , Error "". , "". PHP7 Throwable interface, Exception ( , Exception Throwable, - .: http://php.net/manual/en/language.errors.php7.php).

PHP7 +, 5. * Throwable Error, $foo->modify(); script Fatal Error. (set_error_handler) ( Debug php 5. *), Fatal Errors. Debug script shutdown throws FatalErrorException.

, Symfony, .

+1

Source: https://habr.com/ru/post/1675240/


All Articles