質問

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);
}

Snippet from stack trace:

...Indexer.php(20): sfYamlParser->parse('type: com...') #3 

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.

Error messsage:

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-&gt;parse('type:         s...')
#1 [...]/packages/fabpot-yaml/sfYamlParser.php(201): sfYamlParser-&gt;parse('explicitPrivile...')
#2 [...]/packages/hydra/source/com/daliaIT/hydra/Indexer.php(20): sfYamlParser-&gt;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-&gt;scanCallback('packages/hPacks...', 'hmd', Object(Closure))

EDIT:

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:

why doesnt it fail with "Parsing model definiion '$filePath' failed."?

EDIT:

Turns out PHP has an iteresting way to deal with uncaught exceptions:

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"

役に立ちましたか?

解決

Probably because you don't catch the exception you throw in the catch block.

You have to either stop throwing exception again in the catch block, or make a new try catch around / in the calling method.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top