PHP - 将所有错误转换为异常 - 好还是坏? [英] PHP - Converting all Errors to Exceptions - Good or Bad?
问题描述
我想知道是否将所有PHP错误全局转换为异常被认为是不好的做法。将使用以下内容:
I was wondering if it's considered a bad practice to globally convert all PHP Errors to Exceptions. Something like the following would be used:
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
return false;
}
我假设假设你可以开始使用try / catch围绕通常会抛出错误的某些代码段。
I suppose the assumption is that you can just start using "try/catch" around certain pieces of code that would normally throw Errors.
如果不是Good / Bad的情况,那么这种做法可能会产生什么样的Gotchas?
If it's not a case of Good/Bad, what are some of the Gotchas that could arise from this practice?
推荐答案
不幸的是,这不适用于致命/解析/等等。错误...
Unfortunately, this won't work on fatal/parse/etc. errors...
不记得了,但我已经尝试过,在某些情况下有一个消息,如不能抛出异常没有解决方法... 但是我不记得得到这个结果的条件了。但现在我用这种方式完全满足了。
Don't remember exactly, but I've tried this and in some cases got a message like "can't throw exception without workaround..." but I can't remember the conditions to get this result. But now I use this way and completely satisfied.
这篇关于PHP - 将所有错误转换为异常 - 好还是坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!