note 52255 added to function.trigger-error
| From: | mano at easymail dot hu | Date: | Tue, 26 Apr 2005 12:13:28 +0000 |
| Subject: | note 52255 added to function.trigger-error | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88557@lists.php.net to get a copy of this message | ||
I am not so familiar with this but I think you can reach some of the functionality of try..catch
with this function. I would disagree or correct the previous note. The @ is not the catch, it sets
the error_reporting to 0 tempolary but with set_error_handler you can try to imitate some behavior
like catch.
I've just made this to try if it possible, I never tried in projects but I think could work in
php 4. Actually if you want to have the Trace of the error you need to use php4.3+.
Maybe not many of you will create new scripts in php4 and try to have some features from php5 but in
some cases (extending older projects) this could be better error handling.
If this is a wrong approach, waiting for comments, if its not, use it!
<?
function catch($errno, $errstr, $errfile, $errline){
$GLOBALS['throw'] = $errstr;
$GLOBALS['throw_info'] = array(
'Message'=>$errstr
,'Code'=>$errno
,'Line'=>$errline
,'File'=>$errfile
,'Trace'=>debug_backtrace()
);
}
set_error_handler('catch');
function badgirl(){
if(true){ //error occure
trigger_error("BadGirlWithError",E_USER_NOTICE); // as throw with notice
return; // ...because trigger_error does not stop the function
}else{
return true;
}
}
// Try
$GLOBALS['throw'] = null; // ...try...
$sheis = badgirl(); //could be @badgirl, display_errors is something else, this just the handling
// ..try
if($GLOBALS['throw']){ // This actually catch re-throws too...
switch($GLOBALS['throw']){ // Catch
case 'GoodGirlException':
die('No bad girl today!');
break;
case 'BadGirlWithError':
die('Bad girl has some error');
break;
default:
return; // or you can make another trigger_error and re-throw another 'exception' or
some other action
}
}
echo "You will never see this line - @ only supress message, not a control flow";
echo "Wrong! You could see this:)";
?>
.mano
----
Manual Page -- http://www.php.net/manual/en/function.trigger-error.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52255
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52255&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52255&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52255&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52255&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52255&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52255&report=yes
Search -- http://master.php.net/manage/user-notes.php