PHP Error Handling
1. Grammatical errors
2. Runtime errors
3. Logical error: No error is prompted, but the function is not correct, the most troublesome.
4. Three levels: notice/warning/fatal error (unable to continue execution)
5. Error report shows:
a. The error_reporting project can be modified in php.ini to limit the type of error reporting, such as error_reporting = E_ALL &~E_NOTICE b. Modify only the error display in a script, using error_reporting (E_ALL &~E_NOTICE); (Recommendation)
6. Custom error report: set_error_handle() can be passed in a predetermined parameter to display the error, as follows:
set_error_handler('reportError'); $mess=""; function reportError($error_type,$error_message,$error_file,$error_line){ global $mess; $mess.="The error level is{$error_type}Type, dislocation information<b>{$error_message}</b>,In document{$error_file}In the first place{$error_line}That's ok.<br>"; } getType($a); echo "1111111<br>"; getType(); echo "2222<br>"; echo $mess; /*The error level is 8, and the dislocation information is Undefined variable: a. In the file F: projects Frame FrameTest BackEnd Regular Expression. php, line 24. The error level is of type 2. The error information gettype() expects exactly 1 parameter, 0 given, in line 26 of the file F: projects Frame FrameTest BackEnd Regular Expression. php.*/
7. Logging errors
a. Set display_errors to Off and log_errors to On in PHP.ini b. Custom log directory error_log="C:/XX/XX/php_error.log"
c. You can also use ini_set("display_errors","Off") or ini_get to set it inside the script.
II. PHP exception handling
1.try catch is a whole. There can't be any code in it.
2.Exception is a predefined class of the system
3. If an exception object is thrown, the exception object is given to the class in catch.
4. After the exception position occurs in try, the code no longer continues to execute, but goes directly to catch to execute.
try{ echo "Driving to work<br>"; throw new Exception("The car has a flat tire!"); }catch(Exception $e){//Equivalent to Exception $e = new Exception(''); echo $e->getMessage().'<br>'; echo 'Change into spare tires and continue to work<br>'; }
5. Exception handling can be used in conjunction with error handling
set_error_handler('reportError'); function reportError($error_type,$error_message,$error_file,$error_line){ if($error_type==E_WARNING){ throw new Exception("Error message:{$error_message},Error file:{$error_file},Wrong number of rows{$error_line}"); } } function drive($a){ echo $a; } try{ echo "Driving to work<br>"; drive();//Forget parameters, trigger warning errors in custom error functions, throw exceptions }catch(Exception $e){//Equivalent to Exception $e = new Exception(''); echo $e->getMessage().'<br>'; echo "Change into spare tires and continue to work<br>"; }
6. Customize exception classes
a.Exception class is the base class of all exceptions. There is no specific exception handling method defined (only some methods to get prompts) b. The custom exception class must be a subclass of the system class c. If you continue the Exception class and override the constructor, don't forget to call the parent constructor for initialization
class BTException extends Exception { function __construct($message){ parent::__construct($message); } function method(){ return "Open the trunk, take out the tools and replace the spare tires."; } } try{ echo "Driving to work<br>"; throw new BTException("The car has a flat tire!"); }catch(BTException $e){//Equivalent to Exception $e = new Exception(''); echo $e->getMessage().'<br>'; echo $e->method().'<br>'; echo "Change into spare tires and continue to work<br>"; }
7. Capture multiple exceptions, note: try can also be nested in try
class Err1 extends Exception { function __construct($message){ parent::__construct($message); } function method(){ return "Correct Error 1"; } } class Err2 extends Exception { function __construct($message){ parent::__construct($message); } function method(){ return "Correct Error 2"; } } class Err3 extends Exception { function __construct($message){ parent::__construct($message); } function method(){ return "Correct Error 3"; } } $rand=rand(1,3); try{ switch($rand){ case 1: throw new Err1("Error 1"); case 2: throw new Err2("Error 2"); case 3: throw new Err3("Error 3"); } }catch(Err1 $e){ echo $e->getMessage().'<br>'; echo $e->method().'<br>'; }catch(Err2 $e){ echo $e->getMessage().'<br>'; echo $e->method().'<br>'; }catch(Err3 $e){ echo $e->getMessage().'<br>'; echo $e->method().'<br>'; }