rick645 Posted July 22, 2023 Share Posted July 22, 2023 (edited) First of all $ php -v PHP 8.1.2-1ubuntu2.13 (cli) (built: Jun 28 2023 14:01:49) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.2, Copyright (c) Zend Technologies with Zend OPcache v8.1.2-1ubuntu2.13, Copyright (c), by Zend Technologies https://akrabat.com/turn-warnings-into-an-exception/ Interestingly, in PHP7, we can expect to see exceptions in the engine itself which should allow us to solve this problem like this: try { $result = file_get_contents($url); } catch (EngineException $e) { // do something with $e } Let's try... try { file_get_contents("http://x.y.z"); } catch (EngineException $e) { echo get_class($throwable) . ": CATCHED" . PHP_EOL; } Output PHP Warning: file_get_contents(): php_network_getaddresses: getaddrinfo for x.y.z failed: Temporary failure in name resolution PHP Warning: file_get_contents(http://x.y.z): Failed to open stream: php_network_getaddresses: getaddrinfo for x.y.z failed: Temporary failure in name resolution It seems that catching doesn't work. It appears that the line inside the catch block is not executed. Why? Edited July 22, 2023 by rick645 Quote Link to comment Share on other sites More sharing options...
requinix Posted July 22, 2023 Share Posted July 22, 2023 Because as the first two words of that output show, Quote PHP Warning: those are warning messages - not exceptions. Which suggests that you didn't include in your own code the set_error_handler code that blog post demonstrated. Which, by the way, would throw an ErrorException and not an EngineException. And speaking of, there is no such thing as "EngineException". PHP 7+ converted some things into exceptions. Not all things. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.