<? 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.