tags:

views:

332

answers:

3

Hello everyone,

I am trying to run Example #1 from this page: http://php.net/manual/en/language.exceptions.php

However instead of the desired output I get:

0.2
Fatal error: Uncaught exception 'Exception' with message 'Division by zero.' in xxx:7 Stack trace: #0 xxx(14): inverse(0) #1 {main} thrown in xxx on line 7

I have absolutely no idea, whats wrong... As a developer environment I am using UniServer 3.5 with PHP 5.2.3

+2  A: 

You are not running the identical code, otherwise the exception would be thrown on line 4 instead of 7. What code are you running exactly?

erisco
Code is identical, only with new newlines added.
s7orm
I am unsure of what to say then, as I am unable to reproduce your problem. If you throw an Exception in a try block that has a following catch block that catches an Exception, by all means it should work. It would be a bit more comforting if we had an exact copy of your code.
erisco
<?phpfunction inverse($x) { if (!$x) { throw new Exception('Division by zero.'); } else return 1/$x;}try { echo inverse(5) . "\n"; echo inverse(0) . "\n";} catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n";}// Continue executionecho 'Hello World';?>
s7orm
Suppose you run the code in the CLI (just try the interactive shell), what happens then?
erisco
Everything is fine:W:\usr\local\php>php -f w:\www\test.php0.2Caught exception: Division by zero.Hello WorldAlso when running the code on my website - everything is fine... somehow my test environment is corrupt, but I have no idea how or why
s7orm
Okay, take the nonworking file and add an echo line before the try block. Does this show as output to the browser?
erisco
Yep, it does...
s7orm
I am out of ideas. Looks like outis can help you though. I agree that considering the PHP install and server extension is the next step.
erisco
A: 

My initial though is you have a typo in the name of the exception you are catching/throwing, but if your code is exactly the same I'm not sure exactly what is going on.

Try the following modification of the original script, and paste your results. It will help diagnose your issue a bit better.

<?php

//set up exception handler to report what we didn't catch
function exception_handler($exception) {

    if($exception instanceof MyException) {
        echo "you didn't catch a myexception instance\n";

    } else if($exception instanceof Exception) {
        echo "you didn't catch a exception instance\n";

    } else {
        echo "uncaught exception of type: ".gettype($exception)."\n";
    }

    echo "Uncaught exception: " , $exception->getMessage(), "\n";
}

//install the handler
set_exception_handler('exception_handler');

class MyException extends Exception {
}

function inverse($x) {
    if (!$x) {
        throw new MyException('Division by zero.');
    }
    else return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (MyException $e) {
    echo 'Caught myexception: ',  $e->getMessage(), "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo 'Hello World';
?>
sfrench
+1  A: 

Maybe try disabling certain 3rd party extensions you might have installed? http://bugs.php.net/bug.php?id=41744

chris