try / catch块中未捕获的异常 [英] Uncaught exception in try/catch block
问题描述
一种方法对yaml文件执行递归扫描并解析它们,提取一些信息。我使用sfYamlParser来解析yaml。我在try块中附带了对parse()的调用,并捕获Exception $ e,但是我仍然收到一个致命错误:未捕获的异常。
A method performs a recursive scan for yaml files and parses them, extracts some of the information. I use sfYamlParser to parse the yaml. I enclosed the call to parse() in a try block and catch "Exception $e" but I still get a "Fatal Error: uncaught exception.
try{
$definition = $parser->parse(file_get_contents($filePath));//line 20
} catch(Exception $e) {
throw new Exception("Parsing model definiion '$filePath' failed.", 0, $e);
}
堆栈跟踪中的片段:
...Indexer.php(20): sfYamlParser->parse('type: com...') #3
为什么我的catch块没有捕获异常?
我确实期望异常冒泡,然后被我的方法捕获
该系统命名空间,但使用异常设置。
Why is the exception not caught by my catch block? I did expect the Exception to bubble up and then be caught in my method. The coe is namespaced but "use Exception" is set.
错误消息:
Fatal error</b>: Uncaught exception 'InvalidArgumentException' with message 'Unable to parse line 30 (key; true).' in [...]/packages/fabpot-yaml/sfYamlParser.php:265
Stack trace:
#0 [...]/packages/fabpot-yaml/sfYamlParser.php(201): sfYamlParser->parse('type: s...')
#1 [...]/packages/fabpot-yaml/sfYamlParser.php(201): sfYamlParser->parse('explicitPrivile...')
#2 [...]/packages/hydra/source/com/daliaIT/hydra/Indexer.php(20): sfYamlParser->parse('type: com...')
#3 [...]/packages/co3/source/com/daliaIT/co3/PathHelper.php(97): com\daliaIT\hydra\{closure}('packages/hPacks...')
#4 [...]/packages/hydra/source/com/daliaIT/hydra/Indexer.php(28): com\daliaIT\co3\PathHelper->scanCallback('packages/hPacks...', 'hmd', Object(Closure))
编辑
如果我不抛出另一个异常,我没有发生致命错误。对不起,我期望代码崩溃与我定义的错误消息,不是原始的异常messgae,所以:
Ok if I do not throw another exception I get no fatal error. Sorry I expected the code to crash with the error message I defined, not with the original exception messgae, so:
为什么不会失败与解析模型定义$ filePath 失败?
why doesnt it fail with "Parsing model definiion '$filePath' failed."?
编辑:
处理未捕获的异常的方法:
Turns out PHP has an iteresting way to deal with uncaught exceptions:
如果你抛出一个新的异常(MESSAGE,0,$ previous_exception),并且不抓住它将显示错误消息从$ previous_exception和NOTMESSAGE
If you throw an "new Exception("MESSAGE", 0,$previous_exception)" and do not catch it PHP will display the error message from $previous_exception and NOT "MESSAGE"
推荐答案
可能是因为你没有捕获异常,你会抛出catch块。
Probably because you don't catch the exception you throw in the catch block.
您必须停止在catch块中再次抛出异常,或者在调用方法中/ /在调用方法中新建一个try。
You have to either stop throwing exception again in the catch block, or make a new try catch around / in the calling method.
这篇关于try / catch块中未捕获的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!