1 <?php
2 header("content-type:text/html; charset=utf-8");
3 /**
4 * 包裹重量异常
5 */
6 class HeavyParcelException extends Exception {}
7
8 /**
9 * 包裹类
10 */
11 class Parcel {
12
13 /**
14 * 包裹寄送目的地地址
15 */
16 public $address;
17
18 /**
19 * 包裹重量
20 */
21 public $weight;
22 }
23
24 /**
25 * 派送员
26 */
27 class Courier {
28
29 /**
30 * 运送
31 */
32 public function ship(Parcel $parcel) {
33 //check we have an address
34 //如果包裹的目的地为空
35 if(empty($parcel->address)) {
36 throw new Exception('address not Specified(未填写地址)!');
37 }
38
39 //check the weight
40 //如果重量超过5
41 if($parcel->weight > 5) {
42 throw new HeavyParcelException('Parcel exceeds courier limit(包裹超过运送上限)!');
43 }
44
45 //otherwise we're coll
46 return true;
47 }
48 }
49
50 $myCourier = new Courier();
51 $parcel = new Parcel();
52 //add the address if we have it 为了测试这里不填写地址
53 $parcel->weight = 7;
54 try {
55 $myCourier->ship($parcel);
56 echo "parcel shipped";
57 } catch (HeavyParcelException $e) {//捕获HeavyParcelException 不写这个异常的类型名字,就跑到普通Exception抛出去了
58 echo "Parcel weight error(重量错误): " . $e->getMessage();
59 //redirect them to choose another courier
60 } catch (Exception $e) {
61 echo "Someting went wrong(地址错误): " . $e->getMessage();
62 //exit so we don't try to proceed any further
63 exit;
64 }
65 echo '<br/>';
66 $a = 123;
67 echo $a;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有