PHP Exception Handling

Source: Internet
Author: User
Tags getmessage php exception handling

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

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.

Here are some of the code for my user registration feature

 Try{    //check forms filled in    if(!filled_out ($_post)){        Throw New Exception(' You have not completed the form yet, please go back and fill out '); }    //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 times the password is different, 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. }

Go from: http://www.cnblogs.com/zhmt/archive/2012/03/10/2389347.html

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.