Test environment: PHP5.5.36 Safari 9.1.2
exception capture, in many IDE tools now can use shortcut keys are easy to add, to prevent users from seeing their own errors or even inexplicably crashes, resulting in a bad user experience.
even showing a friendly crash hint is much better than showing error:xxxx xxxxxxxxx directly. Of
Course, the ultimate goal is to give the corresponding solution, so that the code can continue to run.
1<?PHP2 Header("content-type:text/html; Charset=utf-8 ");3 /**4 * Package weight is abnormal5 */6 classHeavyparcelexceptionextends Exception {}7 8 /**9 * Parcel ClassTen */ One classParcel { A - /** - * Parcel Delivery Destination Address the */ - Public $address; - - /** + * Package Weight - */ + Public $weight; A } at - /** - * Dispatch Clerk - */ - classCourier { - in /** - * Shipping to */ + Public functionShip (Parcel$parcel) { - //Check we have an address the //If the destination of the package is empty * if(Empty($parcel-address)) { $ Throw New Exception(' Address not Specified ', not filled in)! ');Panax Notoginseng } - the //Check the weight + //If the weight exceeds 5 A if($parcel->weight > 5) { the Throw NewHeavyparcelexception (' Parcel exceeds courier limit (parcel exceeds shipping limit)! '); + } - $ //otherwise we ' re Coll $ return true; - } - } the - $myCourier=NewCourier ();Wuyi $parcel=NewParcel (); the //Add the address if we have it in order to test here do not fill in addresses - $parcel->weight = 7; Wu Try { - $myCourier->ship ($parcel); About Echo"Parcel Shipped"; $}Catch(heavyparcelexception$e) {//capture Heavyparcelexception does not write the type name of this exception, ran to the ordinary exception thrown away - EchoParcel Weight error (weight error): ".$e-getMessage (); - //redirect them to choose another courier -}Catch(Exception $e) { A Echo"Someting went wrong (address error):".$e-getMessage (); + //exit so we don ' t try to proceed any further the Exit; - } $ Echo' <br/> '; the $a= 123; the Echo $a;
Code execution order starting with 54 lines:
>
>
(The ship method first checks that the address is empty, this throws exception instead of the 57-row heavyparcelexception) >
60 (Capture to Exception) >
616263 output address error exit; 65 to 67 lines are not output
Tips:
I feel that the most important thing about this piece is figuring out the order in which the code executes. write a few paragraphs, then change a run.
1. The order of the captures depends on which type of exception is in the code in the try and then the order in the catch.
2.57 lines of capture are specific types heavyparcelexception can not write wrong, or write exception will have a problem. You can try it yourself.
1) For example, if the address is not empty and 57 lines are written as HeavyParcelException111, it will be captured in 60 rows of its parent class object, with a weight error. That's not what we want.
2) For example, if the address is empty and 57 lines are written as exception, the address error is thrown, but the catch that is responsible for the weight is captured. And that's not what we want.
Throwing exceptions in PHP and catching specific types of exceptions