Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

356
Views
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

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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

about 4 years ago · Santiago Trujillo Report

0

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
}
about 4 years ago · Santiago Trujillo Report

0

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!