Victor Marchuk
Victor Marchuk

Reputation: 13886

try/catch doesn't work in PHP

Why am I getting this error?

Warning: file_get_contents(http://www.example.com) [function.file-get-contents]: failed to open stream: HTTP request failed! in C:\xampp\htdocs\test.php on line 22

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\test.php on line 22

Here is the code:

 try {
    $sgs = file_get_contents("http://www.example.com");
 }
 catch (Exception $e) {
    echo '123';
 }
 echo '467';

Aren't try\catch supposed to continue the excecution of the code? Or maybe there is some different way to do it?

Upvotes: 17

Views: 24786

Answers (7)

cwallenpoole
cwallenpoole

Reputation: 81988

try... catch is more for null object exceptions and manually thrown exceptions. It really isn't the same paradigm as you might see in Java. Warnings are almost deceptive in the fact that they will specifically ignore try...catch blocks.

To suppress a warning, prefix the method call (or array access) with an @.

 $a = array();
 $b = @$a[ 1 ]; // array key does not exist, but there is no error.

 $foo = @file_get_contents( "http://somewhere.com" );
 if( FALSE === $foo ){ 
     // you may want to read on === there;s a lot to cover here. 
     // read has failed.
 }

Oh, and it is best to view Fatal Exceptions are also completely uncatchable. Some of them can be caught in some circumstances, but really, you want to fix fatal errors, you don't want to handle them.

Upvotes: 19

Ondrej Slinták
Ondrej Slinták

Reputation: 31910

file_get_contents doesn't throw exception (and thus errors and warnings it throws aren't catchable). You are getting PHP warning and then fatal error, which explains you why the script doesn't continue - it exceeded limit for loading scripts set in php.ini.

Upvotes: 1

Kumar
Kumar

Reputation: 5147

Fatal errors in PHP are not caught. Error handling and Exception handling are two different things. However if you are hell bent on handling fatal errors as exception, you will need to set up your own error handler and direct all errors to it, make your error handler throw exceptions and you can then catch them.

Upvotes: 1

ChrisLively
ChrisLively

Reputation: 88044

Here are some good details: http://pc-technic.blogspot.com/2010/10/php-filegetcontents-exception-handling.html

Basically change your code to do the following:

try {
    @$sgs = file_get_contents("http://www.example.com"); 
    if ($sgs == FALSE)
    {
       // throw the exception or just deal with it
    }
} catch (Exception $e) {
    echo '123'; 
}
 echo '467';

Note the use of the '@' symbol. This tells PHP to ignore errors raised by that particular piece of code. Exception handling in PHP is very different than java/c# due to the "after the fact" nature of it.

Upvotes: 2

Karoly Horvath
Karoly Horvath

Reputation: 96258

catch cannot catch a fatal error.

Just search for timeout in the manual for file_get_contents, there are several solutions listed there, here is one:

$ctx = stream_context_create(array(
    'http' => array(
        'timeout' => 1
        )
    )
);
file_get_contents("http://example.com/", 0, $ctx); 

Upvotes: 6

karim79
karim79

Reputation: 342635

In PHP a fatal error will halt execution of the script. There are ways to do something when you run into them, but the idea of a fatal error is that it should not be caught.

Upvotes: 2

Flambino
Flambino

Reputation: 18773

try..catch will only catch exceptions. A fatal error is not an exception.

If PHP exceeds its maximum execution time, there's nothing you can do. PHP simply stops dead. It's the same if PHP runs out of memory: Nothing you can do to fix it after it's happened.

In other words, exceptions are errors you can potentially recover from. Fatal errors are, well, fatal and unrecoverable.

Upvotes: 4

Related Questions