鎵╁睍PHP鍐呯疆鐨勫紓甯稿鐞嗙被


鎵╁睍PHP鍐呯疆鐨勫紓甯稿鐞嗙被,鐢ㄦ埛鍙互鐢ㄨ嚜瀹氫箟鐨勫紓甯稿鐞嗙被鏉ユ墿灞 PHP 鍐呯疆鐨勫紓甯稿鐞嗙被銆

鐢ㄦ埛鍙互鐢ㄨ嚜瀹氫箟鐨勫紓甯稿鐞嗙被鏉ユ墿灞 PHP 鍐呯疆鐨勫紓甯稿鐞嗙被銆備互涓嬬殑浠g爜璇存槑浜嗗湪鍐呯疆鐨勫紓甯稿鐞嗙被涓紝鍝簺灞炴у拰鏂规硶鍦ㄥ瓙绫讳腑鏄彲璁块棶鍜屽彲缁ф壙鐨勩傝瘧鑰呮敞锛氫互涓嬭繖娈典唬鐮佸彧涓鸿鏄庡唴缃紓甯稿鐞嗙被鐨勭粨鏋勶紝瀹冨苟涓嶆槸涓娈垫湁瀹為檯鎰忎箟鐨勫彲鐢ㄤ唬鐮併

Example #1 鍐呯疆鐨勫紓甯稿鐞嗙被

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<code>
?>
}
    function __toString();                       // 鍙緭鍑虹殑瀛楃涓
    /* 鍙噸杞界殑鏂规硶 */
 
    final function getTraceAsString();          // 宸叉牸鎴愬寲鎴愬瓧绗︿覆鐨 getTrace() 淇℃伅
    final function getTrace();                  // backtrace() 鏁扮粍
    final function getLine();                   // 杩斿洖鍙戠敓寮傚父鐨勪唬鐮佽鍙
    final function getFile();                   // 杩斿洖鍙戠敓寮傚父鐨勬枃浠跺悕
    final function getCode();                   // 杩斿洖寮傚父浠g爜
    final function getMessage();                // 杩斿洖寮傚父淇℃伅
 
    function __construct($message = null, $code = 0);
 
    protected $line;                            // 鍙戠敓寮傚父鐨勪唬鐮佽鍙
    protected $file;                            // 鍙戠敓寮傚父鐨勬枃浠跺悕
    protected $code = 0;                        // 鐢ㄦ埛鑷畾涔夊紓甯镐唬鐮
    protected $message = 'Unknown exception';   // 寮傚父淇℃伅
{
class Exception
<?php</code>

濡傛灉浣跨敤鑷畾涔夌殑绫绘潵鎵╁睍鍐呯疆寮傚父澶勭悊绫伙紝骞朵笖瑕侀噸鏂板畾涔夋瀯閫犲嚱鏁扮殑璇濓紝寤鸿鍚屾椂璋冪敤 parent::__construct() 鏉ユ鏌ユ墍鏈夌殑鍙橀噺鏄惁宸茶璧嬪笺傚綋瀵硅薄瑕佽緭鍑哄瓧绗︿覆鐨勬椂鍊欙紝鍙互閲嶈浇 __toString() 骞惰嚜瀹氫箟杈撳嚭鐨勬牱寮忋

Example #2 鎵╁睍 PHP 鍐呯疆鐨勫紓甯稿鐞嗙被

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<code><?php
/**
 * 鑷畾涔変竴涓紓甯稿鐞嗙被
 */
class MyException extends Exception
{
    // 閲嶅畾涔夋瀯閫犲櫒浣 message 鍙樹负蹇呴』琚寚瀹氱殑灞炴
    public function __construct($message, $code = 0) {
        // 鑷畾涔夌殑浠g爜
 
        // 纭繚鎵鏈夊彉閲忛兘琚纭祴鍊
        parent::__construct($message, $code);
    }
 
    // 鑷畾涔夊瓧绗︿覆杈撳嚭鐨勬牱寮
    public function __toString() {
        return __CLASS__ . ": [{$this->code}]: {$this->message} ";
    }
 
    public function customFunction() {
        echo "A Custom function for this type of exception ";
    }
}
 
 
/**
 * 鍒涘缓涓涓敤浜庢祴璇曞紓甯稿鐞嗘満鍒剁殑绫
 */
class TestException
{
    public $var;
 
    const THROW_NONE    = 0;
    const THROW_CUSTOM  = 1;
    const THROW_DEFAULT = 2;
 
    function __construct($avalue = self::THROW_NONE) {
 
        switch ($avalue) {
            case self::THROW_CUSTOM:
                // 鎶涘嚭鑷畾涔夊紓甯
                throw new MyException('1 is an invalid parameter', 5);
                break;
 
            case self::THROW_DEFAULT:
                // 鎶涘嚭榛樿鐨勫紓甯
                throw new Exception('2 isnt allowed as a parameter', 6);
                break;
 
            default:
                // 娌℃湁寮傚父鐨勬儏鍐典笅锛屽垱寤轰竴涓璞
                $this->var = $avalue;
                break;
        }
    }
}
 
 
// 渚嬪瓙 1
try {
    $o = new TestException(TestException::THROW_CUSTOM);
} catch (MyException $e) {      // 鎹曡幏寮傚父
    echo "Caught my exception ", $e;
    $e->customFunction();
} catch (Exception $e) {        // 琚拷鐣
    echo "Caught Default Exception ", $e;
}
 
// 鎵ц鍚庣画浠g爜
var_dump($o);
echo " ";
 
 
// 渚嬪瓙 2
try {
    $o = new TestException(TestException::THROW_DEFAULT);
} catch (MyException $e) {      // 涓嶈兘鍖归厤寮傚父鐨勭绫伙紝琚拷鐣
    echo "Caught my exception ", $e;
    $e->customFunction();
} catch (Exception $e) {        // 鎹曡幏寮傚父
    echo "Caught Default Exception ", $e;
}
 
// 鎵ц鍚庣画浠g爜
var_dump($o);
echo " ";
 
 
// 渚嬪瓙 3
try {
    $o = new TestException(TestException::THROW_CUSTOM);
} catch (Exception $e) {        // 鎹曡幏寮傚父
    echo "Default Exception caught ", $e;
}
 
// 鎵ц鍚庣画浠g爜
var_dump($o);
echo " ";
 
 
// 渚嬪瓙 4
try {
    $o = new TestException();
} catch (Exception $e) {        // 娌℃湁寮傚父锛岃蹇界暐
    echo "Default Exception caught ", $e;
}
 
// 鎵ц鍚庣画浠g爜
var_dump($o);
echo " ";
?></code>

浠ヤ笂灏辨槸鎵╁睍PHP鍐呯疆鐨勫紓甯稿鐞嗙被鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=111