0

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.

John Weisz
  • 30,137
  • 13
  • 89
  • 132
  • Works [here](https://eval.in/348050). – hynner May 11 '15 at 17:48
  • @hynner Yes, that's how it _should_ work here as well. I have no stray includes, functions, or ini overrides that would affect PHP's response to uncaught exceptions. – John Weisz May 11 '15 at 17:49
  • I would assume the fatal error you are seeing is not a result of this throw statement and you have another fatal error elsewhere. Perhaps post the full script `test.php` or at the least, strip it down to remove possible errors. – Jonathan Kuhn May 11 '15 at 17:50
  • @JonathanKuhn The question lacked a tiny detail, the _original_ exception message itself. The original message was _"Nem kezdhető új INSERT utasítás"_, and that uncoiled the problem's true nature - Exceptions vs. UTF-8 characters in a file without UTF-8 encoding. I'll edit the question, since it is the _key_ to the solution. – John Weisz May 11 '15 at 18:14

2 Answers2

3

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.

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

Community
  • 1
  • 1
John Weisz
  • 30,137
  • 13
  • 89
  • 132
0

It is possible to you to surround the above call with a try {...} catch block but it will be up to you to declare what behaviour you want for your script.

When caught, an exception is an object that can contain some data (like there with your custom message). You can extract those data according to your will thanks to methods.

If we have a look at http://php.net/manual/fr/class.logicexception.php, we can see that there is a getMessage(...) methods which returns the message you stored.

All you have to do would be :

try {
// the code reaching the one above
}
catch (LogicException $e) {
  echo $e->getMessage();
}

EDIT : But at mentionned above, there must be another error triggered before your custom one since it would not be displaying a "Fatal error"

I hope this helped you, sorry for bad englado :(

Cr3aHal0
  • 809
  • 4
  • 11