What are PHP exceptions? PHP5 provides a new object-oriented error handling method. Exception handling is used to change the normal process of the script when a specified error (exception) occurs. This is called an exception. & Nbsp; general usage: & lt ;? Phpfunctiontest () {thrownew PHP exception details
What are PHP exceptions?
PHP 5 provides a new object-oriented error handling method. Exception handling is used to change the normal process of the script when a specified error (exception) occurs. This is called an exception.
General usage:
GetMessage ();}
- Throw new Exception ('XXX'): throws an Exception.
- Try: the abnormal function should be located in the "try" code block. If no exception is triggered, the code continues as usual. However, if an exception is triggered, an exception is thrown.
Catch: the code block captures exceptions and creates an object containing exception information.
Custom exception class:
class myException extends Exception { public function errorMessage() { $errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile() .': '.$this->getMessage().' is not a valid E-Mail address'; return $errorMsg; }}try { throw new myException($email);}catch (myException $e){ echo $e->errorMessage();}
- The myException class is a custom Exception class and must inherit Exception
- Exception is a built-in Exception class in PHP. by default, the default built-in Exception class in PHP is used. However, we can customize it, custom exception classes that meet your needs.
- When a custom exception class is used, throw new myException also needs to throw a custom exception class name.
- You also need to capture custom exception classes during catch.
- If an exception is thrown and not captured, an error occurs in the PHP code, as shown below:
Fatal error: Uncaught exception 'myException' in D:\AppServ\www\cctv\trunk\index.php:12 Stack trace: #0 {main} thrown in D:\AppServ\www\cctv\trunk\index.php on line 12
PHP default exception class details:
Class exception {protected $ message = 'unknow exception'; // custom exception information protected $ code = 0; // The defined exception code protected $ file; // the name of the abnormal PHP program protected $ line; // PHP row number in which an exception occurs // The Constructor used to pass user-defined exception information and user-defined exception code _ construct ($ message = null, $ code = 0); final function getMessage (); final function getCode (); final function getFile (); final function getLine (); final function getTrace (); // return the exception passing route in array form final function getTraceAsString (); // return the getTrace function information formatted as a string function _ toString (); // reload, used to return output strings}
- The default PHP exception class contains many custom variables and methods.
- $ E-> getMessage generally gets error information
- $ E-> getCode () is generally used to obtain error codes.
- $ E-> getFile () get the wrong file information
- $ E-> getLine () get the wrong number of rows
- $ E-> getTrace () file path after an exception
- _ ToString () can be reloaded to return output strings.
PHP exception class:
- Capture exceptions. you can throw an exception when MYSQL cannot be connected or the service logic is incorrect and receive the exception at the top of the code.
- Exception analysis and processing. When an exception occurs, you can capture the error information and write the log.
- You can return friendly prompts. for example, the WEB side can return the form of a page, or the AJAX side can return the JSON data format.
- Principle: if an exception is thrown, it must be captured; otherwise, an error will be reported in PHP.