[ create a new paste ] login | about

Link: http://codepad.org/t0WtzZ9p    [ raw code | output | fork | 1 comment ]

PHP, pasted on Feb 28:
<?php
// START Boilerplate... Ignore me..
function handle($code, $string, $file, $line, $context) {
    throw new Exception($string);
}

set_error_handler('handle', E_ALL);
// END Boilerplate... Ignore me..


function divide($x, $y) {
    $exception = NULL;

    try {
        try {
            $result = $x / $y;
            echo "result is " . $result . "\n";
        } catch (Exception $e) {
            echo "division by zero!\n";
            throw new Exception('division by zero');
        }
    } catch (Exception $e) {
        $exception = $e;
    }
  
    echo "executing finally clause\n";

    if ( ! is_null($exception))
        throw $e;
}

echo "=====\n";
divide(2, 1);
echo "=====\n";
divide(2, 0);
echo "=====\n";


Output:
1
2
3
4
5
6
7
8
9
10
11
12
=====
result is 2
executing finally clause
=====
division by zero!
executing finally clause

Fatal error: Uncaught exception 'Exception' with message 'division by zero' in /t.php:20
Stack trace:
#0 /t.php(35): divide(2, 0)
#1 {main}
  thrown in /t.php on line 20


Create a new paste based on this one


Comments:
posted by cronk on Jun 6
My soul ripped a little when reading this. All because some PHP core dev(s) never understood exception handling. Sigh.
reply