John Humphreys
John Humphreys

Reputation: 39294

Do I need to try/catch on opendir in PHP due to E_WARNING?

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?

Upvotes: 8

Views: 3969

Answers (4)

Spudley
Spudley

Reputation: 168715

Errors/warnings produced in this way cannot therefore be handled using try/catch because the language infrastructure that handles them dates back to before PHP supported exceptions or try/catch.

You'll only really be able to use try/catch when you're using the more modern PHP modules, which use classes and generate exceptions instead of errors and warnings.

Most of PHP's core functionality is still based around the old error handling mechanism.

The first thing you should be doing with opendir() is checking that the directory exists and can be opened before you attempt to open it; this will prevent you needing to handle error conditions with opendir() itself.

If you are still cautious about warnings being thrown, you can suppress the warnings using the @ symbol. Be aware that this will suppress genuine errors as well.

Alternatively, if you're really bored, you could create a wrapper for the whole thing, which changes the error_reporting() level before making the call and sets it back again afterward. You could even have it throw an exception for you if necessary.

So these things can be done, but it's unlikely to be worth the hassle; you may as well just live with the fact that PHP's error handling isn't great, and has evolved over time rather than being carefully thought out in the first place as C++/C#'s was.

Upvotes: 2

Rusty Fausak
Rusty Fausak

Reputation: 7525

You would use catch in order to deal with a raised Exception. A PHP error is something different. PHP errors have different levels, or severity. You can modify which of these errors is outputted using error_reporting(). In order to suppress an individual PHP error on a statement, use @, like: @opendir(..)

Upvotes: 11

aorcsik
aorcsik

Reputation: 15552

You may suppress the warning with the @ simbol like @opendir(). This is common to suppress resource handler warnings, since there are cases when the resource in unavailable, but these should be handled with care.

In live environment you should have error display off, so it is only useful in developement, but it may break your generated page, so it may be useful to suppress it.

Upvotes: 1

Roger Far
Roger Far

Reputation: 2385

Warnings should only be thrown in test/acceptation environments. You can safely check if the directory exists by strong compared it with false

if (opendir($dir) === false)
    echo "it failed!";

The waring can be caught with a custom error handler which can write it to a log file instead showing it on the screen.

Upvotes: 1

Related Questions