PHP Try/Catch and Fatal Error

PHP try/catch and fatal error

try/catch blocks only work for thrown exceptions (throw Exception or a subclass of Exception must be called). You cannot catch fatal errors using try/catch.

If your DB connection cannot be established, I would consider it fatal since you probably need your DB to do anything meaningful on the page.

PDO will throw an exception if the connection cannot be established. Your specific problem is that $db is not defined when you try to call a method with it so you get a null pointer (sort of) which is fatal. Rather than jump through if ($db == null) hoops as others are suggesting, you should just fix your code to make sure that $db is either always defined when you need it or have a less fragile way of making sure a DB connection is available in the code that uses it.

If you really want to "catch" fatal errors, use set_error_handler, but this still stops script execution on fatal errors.

PHP: What is the difference between an exception and a catchable fatal error?

Fatal errors can not necessarily be caught (they do not throw usual
exceptions)

Prior to version 7, this was the case. Fatal errors used to stop the script dead in its tracks. However, as of version 7, they're now presented as catchable exceptions. This allows you to gracefully recover from pretty significant issues.

However how is a Catchable Fatal Error different from a normal
Exception?

They both implement Throwable, but with different anchor classes:

Throwable
Error
ParseError
...
Exception
RuntimeException
...

And is it handled the same?

Yep, you can catch them, just like Exceptions.

Is a catchable fatal error a
specific type of exception or not?

Depends on your semantics. A catchable fatal error is an exception but it's not an Exception, if you get my meaning. You can differentiate like this;

// "traditional" exceptions
try {
throw new Foo();
} catch (Exception $e) {
}

// v7 catchable fatal errors
try {
$not_an_object->not_a_method();
} catch (Error $e) {
}

// both
try {
} catch (Throwable $e) {
}

Why does PHP throw fatal errors and break with HTTP 500 although using try/catch blocks?

You are not catching because you are tring to catch an \Exception, but what's being thrown it's an \Error.

Considering your error message, I would say you are using PHP >= 7 (you should specificy that, error handling has changed significantly from version 5 to version 7).

On PHP >= 7, most fatal errors are reported not by raising an error, but by throwing an Error object.

So your statement could be rewritten like:

try {
$a = new ClassNotFindable();
}
catch (\Error $e) {
// do your catching
}

Furthermore, both Error and Exception classes implement the Throwable interface, so you could catching that directly:

<?php

try {
$a = new NotFound();
}
catch (\Throwable $t) {
echo "caught!\n";

echo $t->getMessage(), " at ", $t->getFile(), ":", $t->getLine(), "\n";
}

You can see it working here.

This is in no way related to AWS, but simply a PHP thing. If you were using PHP < 7 it would still not be caught, but in that case because common errors are not thrown exceptions.

If you were using PHP5, to be able to catch an error as an exception you'd need to set-up a custom error handler. The example in the manual seems quite appropriate:

function exception_error_handler($severidad, $mensaje, $fichero, $línea) {
if (!(error_reporting() & $severidad)) {
// Este código de error no está incluido en error_reporting
return;
}
throw new ErrorException($mensaje, 0, $severidad, $fichero, $línea);
}
set_error_handler("exception_error_handler");

PHP catch fatal error and redirect

Something like this:

$result = $db->execute($query);

if ($result===false) {
header("Location: errorpage.php");
exit;
}
$primary = $result->getRows();


Related Topics



Leave a reply



Submit