PHP Exception handling class exception use and description _php tutorial

Source: Internet
Author: User
Tags php class php exception handling
1, first PHP5 provides the basic exception handling class, can be used directly
Copy CodeThe code is as follows:
Class Exception
{
protected $message = ' Unknown exception '; Exception information
protected $code = 0; User-defined exception codes
protected $file; The file name of the exception that occurred
protected $line; The line number of the code where the exception occurred
function __construct ($message = null, $code = 0);
Final function getMessage (); Return exception information
Final function GetCode (); Return exception code
Final function getFile (); Returns the file name of the exception that occurred
Final function getLine (); Returns the line number of the code where the exception occurred
Final function gettrace (); BackTrace () array
Final function gettraceasstring (); Gettrace () information that has been rasterized into a string
/* Overloaded method */
function __tostring (); A string that can be output
}
?>

The simple use is as follows: (by exception, throw an error message)
Copy CodeThe code is as follows:
try {
$error = ' my error! ';
throw new Exception ($error)
} catch (Exception $e) {
echo $e->getmessage ();
}

2, we can expand this class, convenient for our use
Copy CodeThe code is as follows:
Class MyException extends Exception
{
Redefine the constructor to make the message a property that must be specified
Public function __construct ($message, $code = 0) {
Custom code
Make sure all variables are correctly assigned
Parent::__construct ($message, $code);
}
Customizing the style of the string output
Public Function __tostring () {
Return __class__. ": [{$this->code}]: {$this->message}\n";
}
Public Function customfunction () {
echo "A Custom function for this type of exception\n";
}
}

The basic idea of exception handling is that code is invoked to execute in the try code. If there is an error in the try code block, we can perform a handling of throwing exceptions. Some programming languages, such as Java, will automatically throw exceptions in certain situations. In PHP, exceptions must be thrown manually. You can throw an exception using the following method:
Throw New Exception (' message ', code);
The Throw keyword triggers an exception-handling mechanism, which is a language structure, not a function, but it must be passed a value. It requires an accepted object. In the simplest case, you can instantiate a built-in exception class.
Finally, after the try code, you must give at least one catch code block. You can associate multiple catch code blocks with a try code block. If each catch code block can catch a different type of exception, it makes sense to use multiple catch blocks. For example, if you want to capture exceptions for the exception class, the code is as follows
Copy CodeThe code is as follows:
Catch (Exception $e)
{
Handing exception
}
The catch code captures an object that causes an exception and is passed to the throw statement (thrown by a 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 that contains the code that generated the exception.

Attention:
When an exception is caught, subsequent code inside the try () block will not continue, but will attempt to find a matching "catch" block of code
When an exception is thrown, the "uncaught exception ' exception '" error is reported if no catch is processed
Copy CodeThe code is as follows:
function test ($val) {
if ($val >100) {
throw new Exception ("Prompt message: You have entered a value too large");
}
}
Test (111);
?>

3. When an exception is thrown, the catch statement block can be processed or not handled
Here are some of the code for my user registration feature
Copy CodeThe code is as follows:
try{
Check forms filled in
if (!filled_out ($_post)) {
throw new Exception (' You have not filled out the form, please go back and fill in ');
}
Check email Address not valid
if (!check_email ($email)) {
throw new Exception (' The message is not in the correct format ');
}
Check if 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 two times if the password is equal
if ($passwd! = $passwd 1) {
throw new Exception (' Two password is not the same, please re-enter ');
}
Check if the user name is the correct length
if (strlen ($username) >16) {
throw new Exception (' The length of the user name does not match, please re-enter ');
}
} catch (Exception $e) {
echo $e->getmessage (); Output exception information.
}

http://www.bkjia.com/PHPjc/325575.html www.bkjia.com true http://www.bkjia.com/PHPjc/325575.html techarticle 1, first PHP5 provides the basic exception handling classes, you can directly use the copy code code as follows:? PHP class Exception {protected $message = ' Unknown Exception ';//exception information protect. ..

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.