I’m making a little file browser for homework – and I have it working as well as I need to, but I’m a little confused on handling error scenarios in PHP now that I’m trying to refactor my coding.
I’m used to C++/C# try/catch error handling, which PHP seems to have some variant of. What’s confusing me is this:
resource opendir ( string $path [, resource $context ] )
Returns a directory handle resource on success, or FALSE on failure.
If path is not a valid directory or the directory can not be opened
due to permission restrictions or filesystem errors, opendir() returns
FALSE and generates a PHP error of level E_WARNING. You can suppress
the error output of opendir() by prepending ‘@’ to the front of the
function name.
from http://php.net/manual/en/function.opendir.php.
Do I need to catch the generated PHP error of level E_WARNING mentioned there, or is it silly to? I don’t understand why it would return false and throw an error – shouldn’t the error throwing make it so you don’t return normally anyway?
You would use
catchin order to deal with a raisedException. A PHP error is something different. PHP errors have different levels, or severity. You can modify which of these errors is outputted usingerror_reporting(). In order to suppress an individual PHP error on a statement, use@, like:@opendir(..)