Re: Exception handling with file()

From: Date: Mon, 11 Feb 2008 13:05:58 +0000
Subject: Re: Exception handling with file()
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-269081@lists.php.net to get a copy of this message
I've just started using Try/catch and found the doc is a bit weak. Best I can tell most, and likely all, our regular system functions do not "throw " and an exception for the Exception handler. Thus, you can put your code in a personal function, with a throw, or use the old fashion way, e.g., if(!$data = file('http://myserver.com/myfile.txt'){ die(foo);}//if it's critical, or whatever if it's not I've started using try/catch a lot and found it very useful. John Papas wrote:
I need to open a remote file with file() and I would like to put it inside a try-catch but as far as I can tell file() does not raise an exception if it fails. The following code: try { $data = file('http://myserver.com/myfile.txt'); $date = substr($data, 0); } catch (Exception $e) { $data = "it failed"; } echo $data; echoes a warning: Warning: file('http://myserver.com/myfile.txt') [function.file]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in.....


« previous php.general (#269081) next »