我正在为家庭作业制作一个小文件浏览器 - 我已经按照我的需要让它正常工作,但是我对 PHP 中的错误场景处理有点困惑,因为我正在尝试重构我的编码。
我习惯于 C++/C# try/catch 错误处理,PHP 似乎有一些变体。令我困惑的是:
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.
来自 http://php.net/manual/en/function.opendir.php .
我是否需要捕获那里提到的 E_WARNING 级别的生成的 PHP 错误,还是这样做很愚蠢?我不明白为什么它会返回 false 并抛出错误 - 错误抛出不应该让你无论如何都不会正常返回吗?
最佳答案
您可以使用 catch
来处理引发的 Exception
。 PHP 错误是不同的东西。 PHP 错误具有不同的级别或严重性。您可以使用 error_reporting()
修改输出这些错误中的哪些错误.为了抑制语句中的单个 PHP 错误,请使用 @
,例如:@opendir(..)
关于php - 由于 E_WARNING,我是否需要在 PHP 中尝试/捕获 opendir?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7541621/