Try-catch suppress?

PHP never ceases to amaze me. Just the other day a colleague discovered that you can suppress error messages reported by file_get_contents() using the try-catch statement. That should’ve raised an eyebrow. In case you missed it here’s the sample code run from php’s interactive console:

Sample code, nothing odd (yet)


qbook:mobilesns quinton$ php -a
Interactive mode enabled

<?php

echo file_get_contents('bleh');

Warning: file_get_contents(bleh): failed to open stream: No such file or directory in /Library/WebServer/workspaces/default/mobilesns/- on line 3

Call Stack:
19.3675 673612 1. {main}() /Library/WebServer/workspaces/default/mobilesns/-:0
19.3914 673676 2. file_get_contents('bleh') /Library/WebServer/workspaces/default/mobilesns/-:3

Dump $_SERVER
Dump $_GET
Dump $_POST

Predictably, the code snippet above confirms that php will report an error if you attempt to open a file that does not exist. You may have noticed, my php installation has some extra debug output because I’m using the XDebug extension. I recommend it. Highly.

This is where it gets weird

Again, from the console. Here we place the same call to read a non-existent file within a try-catch statement.


qbook:mobilesns quinton$ php -a
Interactive mode enabled

<?php

try
{

echo file_get_contents('bleh');

}
catch (Exception $e)
{

echo "Exception is caught";

}

This time around the error is not reported by php nor is the “catch” executed proving that the error message is suppressed. Is it a bug or a side-effect? Anyone have any thoughts on this? “Its not a bug its a feature!” ~ comes to mind :-)

My php version, in case you’re wondering


qbook:mobilesns quinton$ php -v
PHP 5.2.5 (cli) (built: May 27 2008 11:45:48)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
with Xdebug v2.0.4, Copyright (c) 2002-2008, by Derick Rethans
with Zend Debugger v5.2.14, Copyright (c) 1999-2008, by Zend Technologies

Published in: on March 19, 2009 at 10:17 pm  Comments (15)  
Tags: ,

The URI to TrackBack this entry is: http://phpslacker.com/2009/03/19/try-catch-suppress/trackback/

RSS feed for comments on this post.

15 Comments Leave a comment

  1. On failure, file_get_contents() will return FALSE.

    • Erm yes I know that. Look closer at the blog post. With error_reporting=E_ALL and display_errors=1 file_get_contents() is supposed to blurt out a php error BUT when done within a try-catch the error is mysteriously suppressed

  2. Most of PHP does not work with Exceptions yet. file_get_contents returns a FALSE on failure, like Dever already wrote.

    file_get_contents throw a PHP Error in your example, not exception. Therefor, you can’t catch it with a try/catch block.

    Why it surpresses the orginal PHP error… I can’t answer, seems like a bug to me!

    ../Frenck

    • @Dever @Frenck thanks for posting but you both state the obvious

      The question is: Why is the php error suppressed from within the try-catch?

  3. This appears to be a oddity of the interactive console. If you run the same code from a file at the command line it does display the warning. (tested with PHP 5.2.5):
    test.php:

    ~$ php ./test.php

    Warning: file_get_contents(blah) [function.file-get-contents]: failed to open stream: No such file or directory in /home/jaj/public_html/test.php on line 3
    Call Stack
    # Time Memory Function Location
    1 0.0003 48388 {main}( ) ../test.php:0
    2 0.0003 48388 file_get_contents ( ) ../test.php:3

    • @John Well I wouldn’t blame the interactive console just yet. I originally encountered that “bug” by running a cli script just as u did

      It was just for the purpose of the blog post that i presented the issue from the interactive console

  4. Take a look at this in order to change the error into an exception.
    The first comment has a nice code sample:
    set_error_handler(create_function(‘$a, $b, $c, $d’, ‘throw new ErrorException($b, 0, $a, $c, $d);’), E_ALL);

  5. I get a warning with try catch, also on cli
    php 5.2.6

  6. Warning: file_get_contents(bleh): failed to open stream: No such file or directory in….

    qorks perfectly fine for me with 5.2.6-3ubuntu2 when I copy your example into a file and execute it with php test.php.

    • @zeek @Sascha i think it may be a bug introduced by one of the extensions i have installed. I’m using xDebug, Zend Debugger, APC and who knows what else :-)

  7. Xdebug, Apc, tidy, mcrypt + standard extensions are running and working nicely together.

    But your combination of Xdebug and Zend Debugger is a perhaps the problem…

  8. test generates an error as expected on my local machine running 5.2.5

    my guess would also be that one of your debuggers is the culprit

  9. [...] this new entry to his blog Quinton Parker looks at some strangeness he’s found around the try/catch [...]

  10. Get the warning with php 5.2.6-2ubuntu4.1.

  11. [...] this new entry to his blog Quinton Parker looks at some strangeness he’s found around the try/catch [...]


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>