Try/catch continue HTML execution?

Discussion in 'PHP' started by wpc8mry73q, May 23, 2008.

  1. #1
    Hi!

    I've been looking for answers for a few hours now, and gotten nowhere. What I want: When an error occures in the PHP code, a huge chunk of code should be skipped and the execution of PHP (and later HTML) should continue. I found the try/throw/catch method for my problem, but it doesn't seem to work the way I imagined: Sure, it keeps executing PHP code even after the scope, but after the end PHP tag, nothing more is sent to the client.
    I thought that maybe the problem was that the script stops after the first error, but using set_error_handler("errorHandler", E_ALL & ~E_NOTICE) that only echoes the errors, I saw that all errors were printed together with all the HTML after the script.
    Anyway.. here's the code:
    ----------------------------------------
    <?php
    ini_set("error_reporting", 0);
    ini_set("html_errors", 0);

    set_error_handler(create_function('$errno, $errstr', 'throw new Exception($errstr);'), E_ALL & ~E_NOTICE);

    try {
    mysql_select_db("notevenconnected");
    } catch (Exception $e) {
    echo "<p>Caught exception - \"", $e->getMessage(), "\"</p>\n";
    }

    echo "This is printed."
    ?>
    <p>This is NOT!</p>
    </body></html>
    ----------------------------------------
    How can I make the server send the last piece of HTML code? Is there a PHP ini_set()? Is it a bug? According to http://se.php.net/exceptions , as I understand it, everything after exiting the catch tag should work as usual.

    Thanks for reading;
    LTar
     
    wpc8mry73q, May 23, 2008 IP
  2. wpc8mry73q

    wpc8mry73q Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Heey.

    My mistake entirely.. The problem I describe doesn't exist.

    I found a similar problem in the PHP general mailing list where people suggested that there might be a PHP error after the catch part.. and that was exactly my problem. In the example, I use 'echo "This is printed.";' but in my actual file I had "mysql_close();", which generates a fatal error because the connection is never made in the first place and since error_reporting was off and the statement was outside the try area, the HTML code was never sent to the client.

    /LTar
     
    wpc8mry73q, May 23, 2008 IP