php构造函数里抛出异常_抛出异常,传递给构造函数的消息不会出现在PHP错误中...

I have a given class with some methods in it, one of which throws an exception as per the following when certain faulty criteria is met:

PHP

if ($this->mode !== null) {

throw new LogicException('Nem kezdhető új "insert" utasítás.');

}

If I do not handle this error, PHP will show the "Fatal Error" message as expected, but not the custom message that was passed into the first parameter of the constructor of LogicException:

Fatal error: in /home/uxxxxxxxxx/public_html/test.php on line 87

I expected that throwing an exception outside of a try ... catch block would produce the following output in the browser:

Fatal error: Nem kezdhető új "insert" utasítás. in /home/uxxxxxxxxx/public_html/test.php on line 87

If I specify a custom exception handler, it is possible to to display the original message, albeit in a different style. I know how I could mimic the original behaviour of PHP handling catchable fatal errors, but I do think the message should be displayed without requiring that, purely by throwing a non-caught exception.

Note: swapping LogicException to Exception doesn't change anything.

解决方案

Note: I did not include the key to the solution in my initial question, the original message itself. In the question, I translated the message to english in the hope of better readability, but the original one had UTF-8 characters within.

As it turns out, the problem was that the original message,

Nem kezdhető új "insert" utasítás.

contains UTF-8 characters, which break exception handling if used inside a file without UTF-8 encoding. Switching the file encoding accordingly solved the issue.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值