在 PHP 中,我有时会使用 try/catch 捕获一些异常:
try {
...
} catch (Exception $e) {
// Nothing, this is a test that an exception is thrown.
}
通过这种代码,我最终得到了变量$e
它是无缘无故创建的(大量资源),并且 PHP_MD(PHP 混乱检测器)会因为未使用的变量而创建警告。
从 PHP 8 开始,可以使用非捕获 catch。
这是相关的RFC https://wiki.php.net/rfc/non-capturing_catches,以 48 比 1 的投票结果。
现在可以做这样的事情:
try {
readFile($file);
} catch (FileDoesNotExist) {
echo "File does not exist";
} catch (UnauthorizedAccess) {
echo "User does not have the appropriate permissions to access the file";
log("User attempted to access $file");
}
这样,对于某些异常详细信息不相关且异常类型已提供所有必要上下文的边缘情况,无需创建新变量即可捕获异常。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)