Php throws exceptions and captures specific types of exceptions.

Source: Internet
Author: User

Php throws exceptions and captures specific types of exceptions.
Test environment: php5.36 Safari 9.1.2Exception capture can be easily added using shortcut keys in many ide tools to prevent users from seeing errors that they do not understand, or even failing inexplicably, resulting in poor user experience.Even displaying a friendly crash prompt is much better than displaying error: xxxx xxxxxxxxx directly.Of course, the ultimate goal is to provide corresponding solutions so that the code can continue to run.

1 <? Php 2 header ("content-type: text/html; charset = UTF-8 "); 3/** 4 * Abnormal Package Weight 5 */6 class HeavyParcelException extends Exception {} 7 8/** 9 * package class 10 */11 class Parcel {12 13 /** 14 * address of the parcel sending destination: 15 */16 public $ address; 17 18/** 19 * weight 20 */21 public $ weight; 22} 23 24/** 25 * Courier 26 */27 class Courier {28 29/** 30 * transport 31 */32 public function ship (Parcel $ parcel) {33 // check we have an address34 // if the package's destination 35 if (empty ($ parcel-> address) {36 throw new Exception ('address not Specified (not filled in )! '); 37} 38 39 // check the weight40 // if the weight exceeds 541 if ($ parcel-> weight> 5) {42 throw new HeavyParcelException ('parcel exceeds courier limit (the package exceeds the shipping limit )! '); 43} 44 45 // otherwise we' re coll46 return true; 47} 48} 49 50 $ myCourier = new Courier (); 51 $ parcel = new Parcel (); 52 // add the address if we have it do not fill in the address 53 $ parcel-> weight = 7 for test here; 54 try {55 $ myCourier-> ship ($ parcel ); 56 echo "parcel shipped"; 57} catch (HeavyParcelException $ e) {// catch HeavyParcelException. Do not write the type name of this exception, the error 58 echo "Parcel weight error (weight error ):". $ e-> getMessage (); 59 // redirect them to choose another courier60} catch (Exception $ e) {61 echo "Someting went wrong (address error ):". $ e-> getMessage (); 62 // exit so we don't try to proceed any further63 exit; 64} 65 echo '<br/> '; 66 $ a = 123; 67 echo $;

Code execution sequence starting with 54 lines:

55>

32>

35 (in the ship method, the first check is that the address is null, and an Exception is thrown here, rather than the HeavyParcelException in the 57 rows)>

60 (caught Exception)>

616263 output address error exit; no lines 65 to 67 will be output

 

Tips:

I think the most important thing isClarify the code execution sequence.Write a few paragraphs, and then run it again.

1. The capture sequence depends on the type of Exception throw in the try code, and then the catch sequence.

Row 2.57 captures that the HeavyParcelException of a specific type cannot be written incorrectly, or the Exception may be written. You can try it on your own.

1) For example, if the address is not empty, and row 57 is written as HeavyParcelException111, it will be captured in its parent class object in row 60, with a weight error. This is not what we want.

2) For example, if the address is null and 57 rows are written as exceptions, an address error is thrown, but the catch that is originally responsible for weight is captured. This is not what we want.

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.