Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

360
Visualizações
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 Respostas
Responde à pergunta

0

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

about 4 years ago · Santiago Trujillo Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda