Use and description of exception handling class in PHP

Source: Internet
Author: User

1. PhP5 provides the basic exception handling class, which can be directly used.

<? PHP  Class   Exception  {  Protected   $ Message = 'Unknown exception '; //  Exception Information      Protected   $ Code = 0; //  User-defined exceptionCode      Protected   $ File ; //  File Name with exception      Protected   $ Line ; //  Abnormal code line number      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 with an exception.      Final   Function Getline (); //  Returns the line number of the Code with an exception.      Final  Function Gettrace (); //  Backtrace () array      Final   Function Gettraceasstring (); //  Gettrace () information formatted into strings      /*  Reload Method  */      Function _ Tostring (); //  Output string  } ?>

Simple usage: (throws an error message through an exception)

 
Try{$ Error= 'My error! ';Throw New Exception($ Error)}Catch(Exception $ E){Echo $ E->Getmessage ();}

2. We can expand this class to facilitate our use.

 Class Myexception Extends  Exception  {  //  Redefines the constructor to change the message to a required attribute.      Public   Function _ Construct ( $ Message , $ Code = 0 ){  //  Custom Code // make sure all variables are correctly assigned values 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 called and executed in try code. If an error occurs in the try block, we can execute a process that throws an exception. SomeProgramming Language, Such as Java, will automatically throw an exception under certain circumstances. In PHP, the exception must beManually throw. 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 ()-return the Code passed to the constructor. Getmessage ()-return the message passed to the constructor. GetFile ()-the path to the file that generates the Exception Code is Getline ()-the row of the Code that generates the exception is returned.

Note:

    1. When an exception is caught, the subsequent code in the Try () block will not continue to be executed, but will try to find the matched "catch" block.
    2. When an exception is thrown, if no catch processing is performed, the "uncaught exception 'exception' error will be reported.
<?PHPFunctionTest ($ Val){If($ Val> 100){Throw New Exception("Prompt 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 processed.

The following are some codes of the user registration function.

 Try  {  //  Check forms filled in      If (! Filled_out ( $ _ Post  )){  Throw   New   Exception ('You have not entered the form. Please enter it back' );}  //  Check email address not valid       If (! Check_email ( $ Email  )){  Throw   New   Exception ('The mail format is incorrect');}  //  Check whether the density length is greater than 6       If ( Strlen ( $ Passwd <6 )){  Throw   New   Exception ('Density length should be greater than 6' );}  //  Check whether the two passwords are equal       If ( $ Passwd ! =$ Passwd1  ){  Throw   New   Exception ('Two passwords are different. Please enter them again' );}  //  Check whether the length of the user name is correct       If ( Strlen ( $ Username )> 16 ){  Throw   New   Exception ('The length of the user name does not match. Please try again');}}  Catch ( Exception   $ E  ){  Echo   $ E -> Getmessage (); //  Output exception information. }

Reprinted from: http://www.cnblogs.com/zhmt/archive/2012/03/10/2389347.html and http://www.cnblogs.com/yangsen/archive/2012/01/30/2331595.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.