[英]The message for class “Exception” causes PHP Fatal Error
After execute this simple code: 执行以下简单代码后:
$message = 'some text '.inet_pton('119.252.33.171');
throw new \Exception($message);
PHP returns Fatal Error PHP返回致命错误
Fatal error</b>: in ...
This code is expected to return 该代码有望返回
Fatal error: Uncaught Exception: ..
But this is not happening. 但是,这没有发生。 With this message, the error occurs in the class "Exception"!
使用此消息,在“异常”类中发生错误!
Code example 1 from sandbox.onlinephpfunctions.com (comment and uncomment 2 lines of code 3-4 and 6-7) 来自sandbox.onlinephpfunctions.com的代码示例1 (注释和取消注释2行代码3-4和6-7)
Code example 2 from sandbox.onlinephpfunctions.com 来自sandbox.onlinephpfunctions.com的代码示例2
This behavior is noticed when converting many other IPs. 转换许多其他IP时会注意到此行为。 At the moment I solved the problem with the following line before "throw new":
目前,我在“ throw new”之前使用以下行解决了该问题:
$message = preg_replace( '/[^[:print:]\r\n]/', '_', $message);
How to properly escape characters in message for Exception or it is PHP bug? 如何正确地转义异常消息中的字符或它是PHP错误?
My PHP version is 7.2 我的PHP版本是7.2
That would be because inet_pton()
is not meant to ouput strings, but bytes in network order (I don't remember if it's little endian or big endian). 那是因为
inet_pton()
并不是要输出字符串,而是inet_pton()
网络顺序输出字节(我不记得它是小端还是大端)。
Exception messages are not meant to include bytes, but printable characters. 异常消息并不意味着包括字节,而是可打印的字符。 You can, however, put pure bytes into strings.
但是,您可以将纯字节放入字符串中。 These are then decoded to (probably) UTF-8 or other encoding to display readable text.
然后将它们解码为(可能)UTF-8或其他编码以显示可读文本。
The following code 以下代码
$message = inet_pton('119.252.33.171');
var_dump($message);
for($i = 0; $i < strlen($message); $i++)
{
echo ord($message[$i]) . "\n";
}
Produces 产生
string(4) "w�!�"
119
252
33
171
Compare these two lines 比较这两行
throw new \Exception(chr(33)); // Displays stacktrace
throw new \Exception(chr(252)); // Fails to output
My guess would be that an error occurs while PHP is trying to format the string as UTF-8 to print the exception message to stdout, but invalid UTF-8 string is passed in and the write/decoding fails. 我的猜测是,当PHP尝试将字符串格式化为UTF-8以将异常消息打印到stdout时发生错误,但是传入了无效的UTF-8字符串,并且写入/解码失败。
Either way, seems like a bug in PHP. 无论哪种方式,似乎都是PHP中的错误。
You are using inet_pton()
incorrectly because this is what it does: 您使用
inet_pton()
不正确,因为这是这样做的:
inet_pton — Converts a human readable IP address to its packed in_addr representation
You should append the address to the string without calling inet_pton()
. 您应该在不调用
inet_pton()
情况下将地址附加到字符串。
Now, about the error. 现在,关于错误。
The error doesn't occur in all my PHP setups, from 5.5.12 to 7.2.8. 从5.5.12到7.2.8,我的所有PHP设置中均未发生该错误。 This is the error message I got from PHP 5.5.12:
这是我从PHP 5.5.12得到的错误消息:
Fatal error: Uncaught exception 'Exception' with message 'some text wü!«' in C:\wamp\www\so\54584039.php:5 Stack trace: #0 {main} thrown in C:\wamp\www\so\54584039.php on line 5
I got the same "Uncaught exception" error from other versions, confirmed by https://3v4l.org/Klpep . 我从其他版本中得到了相同的“未捕获的异常”错误,已通过https://3v4l.org/Klpep确认。
This means that the error must be caused by a post-processing of the Exception message that occurs in your PHP setup but does not occur in 3v4l.org's setup and mine. 这意味着该错误必须由对PHP设置中发生的Exception消息的后处理引起,而在3v4l.org的设置和我的情况中不发生。
It occurred to me that a possible culprit is the html_errors
configuration option. 在我看来,可能的罪魁祸首是
html_errors
配置选项。 I set it to On
to find out what's going to happen and sure enough, I got your error: 我将其设置为“
On
以了解将要发生的情况,而且确实,我得到了您的错误:
<br />
<b>Fatal error</b>: in <b>C:\wamp\www\so\54584039.php</b> on line <b>5</b><br />
Thus, the solution is to set your html_errors
to Off
. 因此,解决方案是将
html_errors
设置为Off
。
I highly recommend that you set it to Off
always, because if you're working on a JSON output (or other formats) the HTML formatting is useless and it also makes the error message harder to read. 我强烈建议您始终将其设置为“
Off
,因为如果您使用的是JSON输出(或其他格式),则HTML格式是无用的,而且还会使错误消息更难以阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.