View Single Post
Old 06-28-2009, 03:51 AM   #7 (permalink)
Kalle
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 353
Thanks: 8
Kalle is on a distinguished road
Default

PHP doesn't use exceptions as general error handling, however using the builtin ErrorException class you can turn that into reality, but only if PHP emits an error:
php Code:
<?php
/* This is the call back for set_error_handler */
function exception_error_handler($severity, $message, $file, $line )
{
    throw new ErrorException($message, 0, $severity, $file, $line);
}

/* Set the new error handler */
set_error_handler('exception_error_handler');

/* Trigger exception, because this function needs more than 0 parameters */
try
{
    min();
}
catch(ErrorException $e)
{
    switch($e->getSeverity())
    {
        case(E_WARNING):
        {
            printf('Warning: %s in %s on line %d', $e->getMessage(), $e->getFile(), $e->getLine());
        }
        break;
        /* ... same for other error levels thats catchable here ... */
    }
}
?>

and testing:
Code:
C:\> php test.php
Warning: min() expects at least 1 parameter, 0 given in C:\test.php on line 14
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote