Throw exception in try / catch block

The method performs a recursive scan of yaml files and analyzes them, extracts some of the information. I am using sfYamlParser to analyze yaml. I included the parse () call in the try block and caught "Exception $ e", but I still get "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 didn't the exception get into my catch block? I expected Exception to start bubbling, and then get caught in my method. The coefficient is assigned to the names, 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:

Well, if I don't throw another exception, I don't get a fatal error. Sorry, I was expecting the code to crash with the error message I defined, and not with the original exclusive messgae, so:

why doesn't it fail with "Parsing Model Definition" $ filePath "failed". ??

EDIT:

Turns out PHP has a fighter way to handle uncaught exceptions:

If you throw a "new exception" ("MESSAGE", 0, $ previous_exception) and don't catch it, PHP will display an error message from $ previous_exception and NOT "MESSAGE"

0
source share
1 answer

Perhaps because you will not catch the exception that you selected in the catch block.

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

+2
source

Source: https://habr.com/ru/post/974375/


All Articles