PHP Exception Explanation
What is the exception to PHP?
PHP 5 provides a new approach to object-oriented error handling. Exception handling is used to change the normal process of a script when a specified error (exception) condition occurs. This condition is called an exception.
General methods of Use:
GetMessage ();}
- throw new Exception (' XXX '): Throws an exception
- Try: The function that uses the exception should be in the "try" code block. If no exception is triggered, the code will continue to execute as usual. However, if an exception is triggered, an exception is thrown.
Catch: A block of code catches an exception and creates an object that contains the exception information
Custom Exception classes:
Class MyException extends Exception {public Function errormessage () { $errorMsg = ' Error in line '. $this->getline ( ). ' In '. $this->getfile () '. $this->getmessage (). ' was 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 that must inherit exception
- Exception is a PHP built-in exception class that uses PHP's default built-in exception class by default, but we can customize the exception class to meet our own needs in a custom way.
- When you use a custom exception class, throw new MyException throws an exception and also throws a custom exception class class name
- Catch is also required to capture custom exception classes
- If an exception is thrown without capturing processing, a PHP code error will occur, as follows:
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;//definition of the exception code protected $file; Exception PHP Program name protected $line;//The PHP line number of the exception/ /is used to pass the user custom exception information and the user custom exception code constructor function __construct ($ Message=null, $code =0); Final function getMessage (); Final function GetCode (); Final function getFile (); Final function getLine (); Final function gettrace (); Returns the route of the exception pass as an array final function gettraceasstring ();//Returns the Gettrace function information formatted as a String functions __tostring ();//Can be overloaded To return a string that can be output}
- You can see that there are many custom variables and methods in the PHP default exception class
- $e->getmessage is generally getting error messages
- $e->getcode () usually gets the error code
- $e->getfile () Gets the wrong file information
- $e->getline () Gets the wrong number of rows
- File path $e->gettrace () exception
- __tostring () can generally be overloaded to return a string that can be output
The role of PHP exception classes:
- Catching exceptions can usually be thrown at the top of the code when the MySQL link is not available, or when the business logic is wrong.
- Exception analysis processing. You can catch an exception error message and write to the log at the time of the exception.
- You can return friendly hints, such as web-side return page form, or return JSON data format on the Ajax side.
- Principle: If an exception is thrown, it must be captured, or PHP will be error.