How can one ensure proper error handling when using functions like @is_dir() or @opendir() in PHP?

Proper error handling when using functions like @is_dir() or @opendir() in PHP can be ensured by using the error control operator (@) along with try-catch blocks to catch any exceptions that may occur. This allows for graceful handling of errors and prevents them from halting the execution of the script.

try {
    $directory = @opendir('/path/to/directory');
    
    if (!$directory) {
        throw new Exception('Failed to open directory');
    }
    
    // Process directory contents
    
    closedir($directory);
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}