抛出异常,传递给构造函数的消息不会出现在PHP错误中 [英] Throw Exception, message passed into constructor won't appear in PHP error

查看:157
本文介绍了抛出异常,传递给构造函数的消息不会出现在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.');
}

如果我不处理此错误,PHP将显示致命错误消息,而不是传递到 LogicException 的构造函数的第一个参数的自定义消息:

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:


致命错误:在 /home/uxxxxxxxxx/public_html/test.php 87

我预计在 try ... catch 块之外引发异常将产生以下内容在浏览器中输出:

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


致命错误:Nemkezdhetőújinsertutasítás。在 /home/uxxxxxxxxx/public_html/test.php 87

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

如果我指定一个自定义的异常处理程序,可以显示原始消息,尽管是不同的样式。我知道我如何能够模仿PHP处理可捕获的致命错误的原始行为,但我认为应该显示消息,而不需要,纯粹是通过抛出非捕获的异常。

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.

注意:交换 LogicException 异常不会更改任何内容。

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

推荐答案

注意:我的初始问题中没有包含解决方案的密钥,原始邮件本身。在这个问题上,我把这个信息翻译成英文,希望能够更好的阅读,但是原来的这个字符就是UTF-8字符。

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,


Nemkezdhetőújinsertutasítás。

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

包含UTF-8字符,如果在没有UTF-8编码的文件中使用,则会中断异常处理。相关的文件编码切换解决了这个问题。

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.

以下主题有许多质量答案描述了这个问题: PHP 5.4抛出异常 - 看不到有ISO-8859-1编码字符串消息

The following topic has numerous quality answers which describe the issue: PHP 5.4 throw exception - Can't see message with ISO-8859-1 encoded string message

这篇关于抛出异常,传递给构造函数的消息不会出现在PHP错误中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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