18-PHP Exception Handling Mechanism

Source: Internet
Author: User
Tags php exception handling
<? PHP/* // if there is no exception, the common practice is to judge a function. // An exception is an exception that can control multiple functions at a time. Function A () {// call method // Add User $ RES1 = adduser ("shunping"); // modify user $ RES2 = updateuser ("Xiaoming "); if ($ RES2 & $ RES1) {echo "successful! ";} Else {echo" failed! ";}}// Method for adding a User Function adduser ($ username) {if ($ username =" shunping ") {// return true after successful addition ;} else {// failed return false ;}/// modify user function updateuser ($ username) {if ($ username = "Xiaoming") {return true ;} else {return false ;}} A (); */### use the exception mechanism for processing. After an exception is caught, subsequent code in the try block will not be executed. Try {echo "2222222222"; adduser ("shunping"); echo "11111111111"; updateuser ("xiaomingss"); echo "333333333333 "; // catch exception class} catch (exception $ e) {echo "Failure Information = ". $ e-> getmessage ();} function adduser ($ username) {if ($ username = "shunping ") {// added successfully} else {// failed to add throw new exception ("failed to add");} function updateuser ($ username) {if ($ username = "Xiaoming") {// update successful} else {// update failed throw new exception ("update failed ");} }?>

Key points:

1. The code located in the try block will not be executed if one of the codes has a problem.

For example, in the case of PHP's exception handling mechanism, the updateuser () method in the try block obviously cannot be successfully executed. The effect shows that 333333 is not printed.

2. The code in the try block is abnormal.

3. Throw new exception throws an exception.

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.