Home >Backend Development >PHP Tutorial >The use and description of PHP's exception handling class Exception_PHP Tutorial

The use and description of PHP's exception handling class Exception_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:18:02826browse

1. First of all, php5 provides basic exception handling classes. You can directly use

to copy the code . The code is as follows:

class Exception
{
protected $message = 'Unknown exception'; // Exception message
protected $code = 0; // User-defined exception code
protected $file; / / The file name where the exception occurred
protected $line; // The code line number where the exception occurred
function __construct($message = null, $code = 0);
final function getMessage(); // Return Exception information
final function getCode(); // Returns the exception code
final function getFile(); // Returns the file name where the exception occurred
final function getLine(); // Returns the line of code where the exception occurred No.
final function getTrace(); // backtrace() array
final function getTraceAsString(); // getTrace() information that has been formatted into a string
/* Overloadable method*/
function __toString(); // Outputable string
}
?>

Simple use is as follows: (Throw error information through exception)
Copy code The code is as follows:

try {
$error = 'my error!';
throw new Exception ($error)
} catch (Exception $e) {
echo $e->getMessage();
}

2. We can extend this class for convenience Our use
Copy code The code is as follows:

class MyException extends Exception
{
// Redefine The constructor turns message into a property that must be specified
public function __construct($message, $code = 0) {
// Customized code
// Ensure that all variables are assigned correctly
parent::__construct($message, $code);
}
// Customize the style of string output
public function __toString() {
return __CLASS__ . ": [{$this ->code}]: {$this->message}n";
}
public function customFunction() {
echo "A Custom function for this type of exceptionn";
}
}

The basic idea of ​​exception handling is that the code is called and executed in the try code. If an error occurs in the try code block, we can perform an exception handling. Some programming languages, such as Java, will automatically throw exceptions under certain circumstances. In PHP, exceptions must be thrown manually. You can throw an exception in the following way:
Throw new Exception('message',code);
The Throw keyword will trigger the exception handling mechanism. It is a language structure, not a function, but it must be given Pass a value. It requires a receiving object. In the simplest case, one can instantiate a built-in Exception class.
Finally, after the try code, at least one catch code block must be given. You can associate multiple catch code blocks with a try code block. It makes sense to use multiple catch blocks if each catch block can catch a different type of exception. For example, if you want to catch exceptions of the Exception class, the code is as follows:
Copy the code The code is as follows:

Catch(Exception $e)
{
//handing exception
}
The object captured by the Catch code is the object that caused the exception and was passed to the throw statement (thrown by the throw statement). Using an instance of the Exception class is a good choice. The
Exception class provides the following built-in methods:
Getcode() —Returns the code passed to the constructor.
GetMessage() — Returns the message passed to the constructor.
getFile() —Returns the path to the file that generated the exception code
getLine() —Returns the line where the code that generated the exception is located.

Note:
When an exception is caught, subsequent code in the try() block will not continue to execute, but will try to find a matching "catch" code block
When an exception is thrown, if no catch is performed, the "Uncaught exception 'Exception'" error will be reported
Copy code The code is as follows:

function test($val){
if ($val>100){
throw new Exception("Message: The value you entered is too large") ;
}
}
test(111);
?>

3. When an exception is thrown, the catch statement block can be processed or not Processing
The following is part of the code for my user registration function
Copy the code The code is as follows:

try{
//check forms filled in
if(!filled_out($_POST)){
throw new Exception('You haven't filled in the form yet, please go back and fill it in');
}
//check email address not valid
if(!check_email($email)){
throw new Exception('The format of the email is incorrect');
}
//Check whether the length of the density is greater than 6
if(strlen($passwd<6)){
throw new Exception('The length of the density should be greater than 6');
}
//Check Are the two passwords equal? ​​
if($passwd!=$passwd1){
throw new Exception('The two passwords are different, please re-enter');
}
//Check the user name Is the length correct?
if(strlen($username)>16){
throw new Exception('The length of the username does not match, please re-enter');
}
} catch(Exception $e){
echo $e->getMessage(); //Output exception information.
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325575.htmlTechArticle1. First, php5 provides basic exception handling classes, which can be directly used. The copied code is as follows: ?php class Exception { protected $message = 'Unknown exception'; // Exception message protect...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn