Php exception handling example. Copy the code as follows :? Php disables error output error_reporting (0); sets the Error Processor set_error_handler (errorHandler); register_shutdown_function (fatalErrorHandler );
The code is as follows:
// Disable error output
Error_reporting (0 );
// Set the Error Processor
Set_error_handler ('errorhandler ');
Register_shutdown_function ('fatalerrorhandler ');
Class Test {
Public function index (){
// Here, a warning error occurs, starting with errorHandler.
Echo $ undefinedVarible;
}
}
Function errorHandler ($ errno, $ errstr, $ errfile, $ errline ){
$ Arr = array (
'['. Date ('Y-m-d h-I-s'). ']',
'Http: // www.baidu.com ',
'| ',
$ Errstr,
$ Errfile,
'Line: '. $ errline,
);
// Write error logs
// Format: time uri | the row where the error message file is located
Error_log (implode ('', $ arr)." \ r \ n ", 3, './test.txt', 'Extra ');
Echo implode ('', $ arr)." \ r \ n ";
}
// Catch fatalError
Function fatalErrorHandler (){
$ E = error_get_last ();
Switch ($ e ['type']) {
Case E_ERROR:
Case E_PARSE:
Case E_CORE_ERROR:
Case E_COMPILE_ERROR:
Case E_USER_ERROR:
ErrorHandler ($ e ['type'], $ e ['message'], $ e ['file'], $ e ['line']);
Break;
}
}
$ Test = new Test ();
//// A warning error occurs, which is captured by errorHandler.
$ Test-> index ();
// A fatal error occurs. if the script stops running, fatalErrorHandler is triggered.
$ Test = new Tesdt ();
$ Test-> index ();
The http://www.bkjia.com/PHPjc/736856.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/736856.htmlTechArticle code is as follows :? Php // disable error output error_reporting (0); // Set the Error Processor set_error_handler ('errorhandler'); register_shutdown_function ('fatalerrorhandler ');...