SexyMF
SexyMF

Reputation: 11155

Can't 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 am not entering to the catch area.
Any idea why?

update

This is the error thrown:

[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 catched

Thanks

Upvotes: 13

Views: 18351

Answers (3)

Saeed Prez
Saeed Prez

Reputation: 778

It does not catch the exception because you are trying to catch \Exception which Symfony\Component\Debug\Exception\FatalThrowableError does not extend.

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

use Symfony\Component\Debug\Exception\FatalThrowableError;

And then you can do..

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

Edit

Ok, so in addition to the above solution it seems PHP 7+ handles error a bit differently than PHP 5. So try this..

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

Upvotes: 6

Cliff Edge
Cliff Edge

Reputation: 143

Symfony's Debug component is much more sophisticated in order to log and report all kinds of errors but take look at this simple example (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';
}

What will be the outcome? Well:

Fatal error: Uncaught MyUncatchableError: BANG: Call to a member function modify() on boolean in /in/WJErU:6

Stack trace:

  • 0 [internal function]: myExceptionHandler(Object(Error))
  • 1 {main}

    thrown in /in/WJErU on line 6

and you can't catch that exception because you should catch the original.. throwable here, which is Error for this kind of "error". You can catch it by catching "Error" class. And with PHP7 hierarchy it implements Throwable interface, that's why you can't catch it using Exception (because while Exception implements Throwable, Error is no an Exception - see: http://php.net/manual/en/language.errors.php7.php).

And this is true for PHP7+ because with 5.* there was no Throwable nor Error, and doing $foo->modify(); would just stop the script and return a Fatal Error. You can make your own error handler (set_error_handler) and throw an exception there (and Debug component does that for php 5.*) but this method does not work for Fatal Errors. Instead Debug component hooks into script shutdown and reads last error and throws FatalErrorException.

This description may not be completely accurate as I have't dug deeply into Symfony but you can get the idea here.

Upvotes: 1

SexyMF
SexyMF

Reputation: 11155

Catching Throwable did the trick.
Have no idea why? Anyone does?

Upvotes: 21

Related Questions