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.