Yaf_Dispatcher::catchException

(Yaf >=1.0.0)

Yaf_Dispatcher::catchException開(kāi)啟/關(guān)閉自動(dòng)異常捕獲功能

說(shuō)明

public Yaf_Dispatcher::catchException(bool $flag = ?): Yaf_Dispatcher

當(dāng) application.dispatcher.throwException 開(kāi)啟的時(shí)候(你也可以通過(guò)調(diào)用 Yaf_Dispatcher::throwException(TRUE)() 來(lái)開(kāi)啟它),Yaf將會(huì)拋出異常而不是觸發(fā)異常發(fā)生。

如果開(kāi)啟了 Yaf_Dispatcher::catchException() (可以通過(guò)設(shè)置application.dispatcher.catchException來(lái)開(kāi)啟),并且在你定義了異常處理的controller的情況下,Yaf會(huì)將所有未捕獲的異常交給Error Controller的Error Action來(lái)處理。

參數(shù)

flag

bool

返回值

范例

示例 #1 Yaf_Dispatcher::catchException()example

/* if you defined a ErrorController like following */
<?php
class ErrorController extends Yaf_Controller_Abstract {
     
/** 
      * you can also call to Yaf_Request_Abstract::getException to get the 
      * un-caught exception.
      */
     
public function error($exception) {
        
/* error occurs */
        
switch ($exception->getCode()) {
            case 
YAF_ERR_NOTFOUND_MODULE:
            case 
YAF_ERR_NOTFOUND_CONTROLLER:
            case 
YAF_ERR_NOTFOUND_ACTION:
            case 
YAF_ERR_NOTFOUND_VIEW:
                echo 
404":"$exception->getMessage();
                break;
            default :
                
$message $exception->getMessage();
                echo 
0":"$exception->getMessage();
                break;
        }
     } 
}
?>

以上例程的輸出類似于:

/* now if some error occur, assuming access a non-exists controller(or you can throw a exception yourself): */
404:Could not find controller script **/application/controllers/No-exists-controller.php

參見(jiàn)