简体   繁体   English

如何在zend框架php中使用异常处理?

[英]How to use exception handling in zend framework php?

try {
    if ($user_session_object['device_type'] == '1') {
        $this->sendNotificationAndroidOne($notificationData);
    }
    throw new Exception("newInner");
    //throw new Exception("newInner");
    if ($user_session_object['device_type'] == '2') {
        $this->sendNotificationIphone($notificationData);
    }
}catch(Exception $ex){
    echo 'Exception occur while send push notification';
    exit;
}

Getting error: Exception' not found in file. 获取错误:在文件中找不到“异常”。 Thanks in advance 提前致谢

This is likely a namespace issue (nothing to do with Zend Framework), so you want throw new \\Exception("newInner"); 这可能是命名空间问题(与Zend Framework无关),所以你想要throw new \\Exception("newInner"); and catch(\\Exception $ex) . catch(\\Exception $ex)

Otherwise, please add the full error message to your question. 否则,请在您的问题中添加完整的错误消息。

As per your code " throw new Exception("newInner") " it have not makes any sence inside try{} block, because whenever an exception will be found catch(){} block will be called. 根据你的代码“ throw new Exception("newInner") ”它没有在try{}块内部产生任何意义,因为每当发现异常时都会调用catch(){}块。

You should use try---catch like below. 你应该使用try---catch如下所示。

     try{
        if ($user_session_object['device_type'] == '1') {
            $this->sendNotificationAndroidOne($notificationData);
        }           
        if ($user_session_object['device_type'] == '2') {
            $this->sendNotificationIphone($notificationData);
        }
    }catch (\Exception $e) {
        var_dump($e->getMessage());
        var_dump($e->getCode());
        exit;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM