php core technology and best practices --- errors and exceptions

<? PHP
 / * PHP error * / 
/ * 
 * exceptions and errors are not the same concept 
 * in PHP, encounter any errors itself will trigger an error, instead of throwing an exception (for some cases, and at the same time throw an exception error) 
 * is the exception handling mechanism "described in the normal course of doing code" and "code is a problem of how to do" isolated 
 * / 
$ a = null ;
 the try {
    / * $ a = 5/0; * / 
    echo  $ A ; 
} the catch ( Exception  $ E ) {
     $ E -> the getMessage ();
     $ A = -1 ; 
} 
echo  $ A ;
 // Warning: Division by ZERO in C: \ phpStudy \ PHPTutorial \ the WWW \ phpmain \ demo.php on line 10
/ * 
 * You can see from the above operating results, in addition to zero for this "abnormal" situation, PHP think this is a mistake, directly trigger an error, but does not automatically throw an exception 
 * the program goes to an abnormal process, it is ultimately a values are not expected in the -1 
 * in other words, did not enter the hook, there is no exception handling. 
 * Php only after you take the initiative to throw, to catch the exception (generally the way, there are some anomalies can automatically capture) 
 * For java, zero is considered part of ArithmeticException, will be captured, and deal with exceptions. 
 * In other words, php usually make meaningful exception can not be automatically captured, and it all abnormal conditions are deemed wrong, 
 * you want to catch the exception, you have to use if ... else structure to ensure that the Code normal, then determine if the divisor is 0, then an exception is thrown manually, recapture. 
 * Note: In fact, the reason why this gap, the fundamental reason lies between php and java, the java, the exception is the only error reporting, 
 * but in php is not the case. Plainly speaking, this is the differences of the two languages exceptions and errors defined. What is unusual, what is wrong, 
 * there are different views two kinds of language designers. 
 * That is when, php manual can only catch the exception after exception is thrown, or there is a built-in exception mechanism, will first trigger an error, then catch the exception. 
 * / 
Class emailException the extends  Exception { 

} 
class pwdExceptionextends Exception{
    function __toString(){
        return "<div class='error'>Exception {$this->getCode()}:{$this->getMessage()}
                :in File:{$this->getFile()}on line:{$this->getLine()}</div>";
        // 改写异常结果
    }
}
function reg($reginfo = null){
    if(empty($reginfo) || !isset($reginfo)){
        throw new Exception// This does not capture the exception 
    // REG ([]) will trigger an exception is captured and a third catch block. Output exception information: Parameter illegal
     
     


    REG ([ 'In Email' => '', 'pwd' => 12345678, 'repwd' => 12345678 ]);
     // exception is triggered and the catch block is first captured. Output exception information: 
    // Exception 'emailException' with the Message 'message is empty' in C: \ phpStudy \ PHPTutorial \ WWW \ phpmain \ demo.php: 46 Stack trace: # 0 C: \ phpStudy \ PHPTutorial \ WWW \ phpmain \ demo.php (57 is): REG (the Array). 1 # {main} 
    / * REG ([ 'In Email' => '123', 'pwd' => 12345678, 'repwd' => 123456789]); * / 
    // exception will be triggered and the captured second catch block. Abnormality information output: 
    // 0 Exception: two passwords do not match: in File: C: \ phpStudy \ PHPTutorial \ the WWW \ phpmain \ demo.phpon Line: 49 

} the catch (emailException $ E ) {
     echo  $ E ;) {
     Echo  $ EP ; 
} the catch ( Exception  $ E ) {
     echo  $ E -> the getMessage (); 
} 
// NOTE: When the function abnormality occurs, will not continue to perform a down. Only execute corresponding contents catch block. 
// multiple treatments when writing catch, if there is a parent-child relationship classes, subclasses in front, in the back of the parent class 
// because abnormal objects from top to bottom to catch testing, test parameter list of objects we throw whether the same object type, and whether the parent (target catch block) - promoter (thrown object) class relationships; 
// if the third catch block into a first position, the two catch blocks behind also never be executed 
// here, the form of exception handling, by overriding the exception class, throw the wrong way manual exception handling. This is a business exception can be considered to meet the requirements of all cases are not treated as a business exception 
code on // and the usual sense abnormal phase difference 
// If the code is only symbolic try ... catch, then a printing error, the last over. This anomaly is better not, because it does not reflect abnormal thinking. Therefore, the following reasonable code 
/ * the try { 
    possibility of error code segment 
    if (file upload is unsuccessful) the throw (uploads abnormal);
    if (data is not successfully inserted into) the throw (database operation exception); 
} the catch (Exception) { 
    necessary remedial measures, such as file deletion, insertion database record, the process is very delicate 
} * / 

/ * error * / 
// PHP Error handle much larger than the value of the exception. php errors and exceptions concept has done more, here on oho cognitive abnormalities, php error to the next most popular most intuitive conclusion: php error that will 
not run under normal circumstances script // 
// There is a php sets of error-handling mechanism, can be used to take over set_error_handler php error handling, you can use the active trigger_error function throws an error 

// custom error handler 
function customError ( $ errno , $ errstr , $ errfile , $ errline ) {
     echo "custom . " $ errno . ';'. $ The errstr . ';'. $ errfile . ';'. $ errline ; 
}
set_error_handler (" customError " 
captured into //, is a semi-automated exception handling mechanism);
 1/0;   // Custom 2; Division by ZERO; C: \ phpStudy \ PHPTutorial \ the WWW \ phpmain \ demo.php; 96 
// set_error_handler () function will take over the php built-in error message, you can at the same a page using restore_error_handler () to cancel takeover 
// Note: If you use a custom set_error_handler takeover php error messages, previously code error suppression @ will fail, this error will be displayed 
// php seen as an exception to a lot of mistakes so we can put these "anomalies" with the same error as set_error_handler took over, and then take the initiative to throw an exception 
// prompt, if you use this function, completely bypassing the standard PHP error handler, if necessary, user-defined error handler We must terminate (die ()) script 
trigger_error ( 'test'); // define 1024; test; C: \ phpStudy \ PHPTutorial \ the WWW \ phpmain \ demo.php; 99 

// in php, errors and exceptions are two different concepts, this design leads to abnormal PHP and other languages differ fundamentally. 
// to java, for example, in java, the only exceptions are wrong reporting. After all, the difference between the two is the understanding of the different exceptions and errors arising. Most abnormalities php must be manually thrown by some way, in order to be 
// whether an error or abnormal, you can use an existing handler takes over handling mechanism

 

Guess you like

Origin www.cnblogs.com/cl94/p/11361573.html