PHP5 exception handling mechanism [3] - PHP5 error processed Trigger

xiaoxiao2021-03-06  87

We may be able to generate a user warning with Trigger_Error () to make the program more flexible. INDEX2.PHP

cmddir} / {$ CMD} .php "; if (! File_exists ($ PATH)) {Trigger_ERROR (" Cannot Find $ PATH ", E_USER_ERROR);} Require_once $ path; if (! Class_exists ($ cmd)) {Trigger_ERROR (" Class $ cmd does NOT EXIST ", E_USER_ERROR);} $ RET = New $ cmd (); if (! is_A ($ RET, 'Command')) {Trigger_ERROR (" $ cmd is not a command ", e_user_error);} Return $ Ret; }}?>

If you use the Trigger_Error () function to replace Die (), your code will be more advantageous in handling errors, which is more easier to handle errors for customer programmers. Trigger_ERROR () accepts an error message and a constant as a parameter. Constants are:

Constant Meaning E_USER_ERROR A FATAL ERROR E_USER_WARNING A NON-FATAL ERROR E_USER_NOURE A Report That May Not Repesent An Error

You can design an error handler, then define a processor selection function set_error_handler () to use this error handler. Index2.php second half of the half

// PHP 4

Function Cmderrorhandler ($ Errnum, $ Errmsg, $ FILE, $ LINENO) {

IF

$ Errnum == E_USER_ERROR) {

Print

"Error: $ Errmsg / N";

Print

File: $ file / n ";

Print

"Line: $ LINENO / N";

Exit ();}}

$ HANDLER = set_error_handler ('cmderrorhandler');

$ mgr = new commandManager ();

$ cmd = $ mgr-> getcommandObject ('realcommand');

$ cmd-> execute ();

?>

转载请注明原文地址:https://www.9cbs.com/read-122593.html

New Post(0)