Try Catch Cannot Work with Require_Once in PHP

Try Catch cannot work with require_once in PHP?

You can do it with include_once or file_exists:

try {
if (! @include_once( '/includes/functions.php' )) // @ - to suppress warnings,
// you can also use error_reporting function for the same purpose which may be a better option
throw new Exception ('functions.php does not exist');
// or
if (!file_exists('/includes/functions.php' ))
throw new Exception ('functions.php does not exist');
else
require_once('/includes/functions.php' );
}
catch(Exception $e) {
echo "Message : " . $e->getMessage();
echo "Code : " . $e->getCode();
}

How to catch require_once/include_once exception?

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>";
}
}

Function require_once doesn't show the exception message according to the custom function

According to the documentation

require[_once] is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error

Your if check is not only returning false, but also causing a fatal error which stops execution, so it never gets to the else

You should either use include instead (which only causes a WARNING) or file_exists to check if the module exists... since you are handling the error yourself there is no benefit to using require over include. Also, just for better practice I would recommend throwing an exception instead of returning an error string.

Full code:

if ( file_exists( $_module_file ))
include_once( $_module_file );
else
throw new Exception("error...");

Or to simplify things, if you're ignoring WARNING level errors

if( !include_once( $_module_file ))
throw new Exception("error...");

require/require_once ../ breaks loading of page without throwing error

I personally wouldn't expect a relative path to work in any particular way. The problem is that traditionally (i.e. in most OS functions), the path is interpreted relative to the current working directory (CWD) of the process but here you might expect it to work relative to the current file's directory. Instead of using a relative path, be explicit and use __DIR__ to specify the current file's directory:

require_once __DIR__ . '/../dir2/file2.php';

php exception not catching

That's not an exception (but a warning), so you can't simply catch it.

You can suppress warning instead (not recommended in this scenario) or use something to verify if file exists

So, something like

try {
if(!file_exists("jj.php")) {
throw new Exception("File doesn't exists");
}
require_once "jj.php";
return true;
} catch (Exception $ex) {
// if you would you can handle exception here or you can simply
// throw exception without try - catch block
}


Related Topics



Leave a reply



Submit