yii2:抛出新异常的正确方法 [英] yii2: Proper way to throw new exception

查看:64
本文介绍了yii2:抛出新异常的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是为了测试,我在我的模型中添加了这段代码,同时设置了 debug = true 和 false.

Just for testing I have added this code in my model while setting the debug = true and false.

if($packagedays < 1)
     {
      throw new yiiaseException( "package days cannot be less than 1" );
     }

现在当 Yii 调试为真时:我得到了

Now when Yii debug is true: I am getting

异常 – yiiaseException包裹天数不能少于 1

但是当我将调试设置为 false 时我正进入(状态例外发生内部服务器错误.

But when I am setting the debug to false I am getting Exception An internal server error occurred.

上述错误发生在 Web 服务器处理您的请求时.

The above error occurred while the Web server was processing your request.

我想要的是替换发生内部服务器错误.包天数不能小于1debug=false

我在这里缺少什么?

谢谢.

推荐答案

你应该简单地抛出一个 UserException :

You should simply throw a UserException :

UserException 是旨在向最终用户显示的异常的基类.

UserException is the base class for exceptions that are meant to be shown to end users.

阅读更多:http://www.yiiframework.com/doc-2.0/yii-base-userexception.html

这篇关于yii2:抛出新异常的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆