Jump to content

Recommended Posts

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 by rick645
Link to comment
https://forums.phpfreaks.com/topic/317110-engineexception-not-catch/
Share on other sites

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.