I'm trying to write code similar this:
<?php
$includes = array(
'non_existing_file.php', //This file doesn't exist.
);
foreach ($includes as $include) {
try {
require_once "$include";
} catch (Exception $e) {
$message = $e->getMessage();
echo "
<strong>
<font color=\"red\">
A error ocurred when trying to include '$include'.
Error message: $message
</font>
</strong>
";
}
}
I'd tried require_once
and include_once
, but try catch
doesn't catch the exception.
How could I catch this fatal errors/warning exceptions?
This solution (adapted from here) worked fine to me:
register_shutdown_function('errorHandler');
function errorHandler() {
$error = error_get_last();
$type = $error['type'];
$message = $error['message'];
if ($type == 64 && !empty($message)) {
echo "
<strong>
<font color=\"red\">
Fatal error captured:
</font>
</strong>
";
echo "<pre>";
print_r($error);
echo "</pre>";
}
}