The use and description of PHP exception handling class exception _php tips

Source: Internet
Author: User
Tags exception handling getmessage php exception handling
1, first PHP5 provides the basic exception handling class, can use directly
Copy Code code as follows:

<?php
Class Exception
{
protected $message = ' Unknown exception '; Exception information
protected $code = 0; User-defined exception codes
protected $file; Name of the exception that occurred
protected $line; 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 where the exception 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 formatted as a string
/* Can be overloaded method * *
function __tostring (); A string that can be exported
}
?>

The simple use is as follows: (through exception, throw error message)
Copy Code code as follows:

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

2, we can extend this class, to facilitate our use
Copy Code code as follows:

Class MyException extends Exception
{
Redefine the constructor so that the message becomes a property that must be specified
Public function __construct ($message, $code = 0) {
Custom code
Make sure all variables are assigned correctly
Parent::__construct ($message, $code);
}
Custom string Output style
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 the code is invoked to execute in the try code. If an error occurs in the try block, we can perform a handle that throws an exception. Some programming languages, such as Java, will automatically throw exceptions in certain cases. In PHP, exceptions must be thrown manually. You can throw an exception by using the following method:
Throw New Exception (' message ', code);
The Throw keyword triggers the 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. Multiple catch code blocks can be associated with a try block of code. If each catch code block can catch a different type of exception, it makes sense to use multiple catch blocks of code. For example, if you want to catch the exception of the exception class, the code follows
Copy Code code as follows:

Catch (Exception $e)
{
Handing exception
}
The object caught by the catch code is the object that caused the exception and passed to the throw statement (thrown by the throw statement). It is a good choice to use an instance of the exception class.
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" code block
When an exception is thrown, the "uncaught exception ' exception" error is reported if no catch processing is performed
Copy Code code as follows:

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

3. When an exception is thrown, catch statement blocks can be processed or not processed
Here are some of the code for my user registration feature
Copy Code code as follows:

try{
//check forms filled in
if (!filled_out ($_post)) {
Throw New Exception (' You have not yet filled out the form, please go back to fill in ');
}
//check email address not valid
if (!check_email ($email)) {
throw new Exception (' Incorrect format of message ');
}
//Check that the length of the density is greater than 6
if (strlen ($passwd <6)) {
throw new Exception (' density should be longer than 6 ');
}
//Check two times password is equal
if ($passwd!= $passwd 1) {
throw new Exception (' two times password not the same, please re-enter ');
}
//Check that the length of the user name is correct
if (strlen ($username) >16) {
throw new Exception (' username length does not match, please re-enter ');
}
catch (Exception $e) {
echo $e->getmessage ();//output exception information.
}

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.