Php exception handling

Source: Internet
Author: User
Tags php exception handling
The basic idea of php exception handling is that the code is called and executed in try code. If an error occurs in the try block, we can execute a process that throws an exception. Some programming languages, such as java, will automatically throw exceptions under certain circumstances. In php, an exception must be thrown manually. You can throw an exception as follows:

Throw new Exception ('message', code );

The Throw keyword triggers the exception handling mechanism. it is a language structure, not a function, but must pass a value to it. It requires an acceptance object. In the simplest case, you can instantiate a built-in Exception class.

Finally, after the try code, at least one catch code block must be provided. Multiple catch code blocks can be associated with a try code block. If each catch code block can capture a different type of exception, it makes sense to use multiple catch code blocks. For example, if you want to catch exceptions of the Exception class, the code is as follows:

Catch (Exception $ e)

{

// Handing exception

}

The object captured by the Catch code is the object that causes an exception and is passed to the throw statement (thrown by the throw statement ). Exception class instances are 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 of the file that generates the exception code.

GetLine ()? Returns the row of the code that generates the exception.

The following are some codes of the user registration function.

View code

1 try {2 // check forms filled in 3 if (! Filled_out ($ _ POST) {4 throw new Exception ('you have not entered the form yet, please enter it back'); 5} 6 // check email address not valid 7 if (! Check_email ($ email) {8 throw new Exception ('invalid email format '); 9} 10 // check whether the density length is greater than 611 if (strlen ($ passwd <6) {12 throw new Exception ('density length should be greater than 6 '); 13} 14 // check whether the two passwords are equal 15 if ($ passwd! = $ Passwd1) {16 throw new Exception ('Two passwords are different. please enter them again '); 17} 18 // check whether the username length is correct 19 if (strlen ($ username)> 16) {20 throw new Exception ('username length does not match, please enter it again '); 21} 22 23} catch (Exception $ e) {24 echo $ e-> getMessage (); // output Exception information. 25}

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.