2

In php 5.4 my code dont work properly. I use cyrillic charset. In short:

throw new Exception('Сообщение');

will output:

Fatal error: in test.php ...

although the result would be:

Fatal error: Uncaught exception 'Exception' with message ...

If I dont use cyrillic characters, the result is Ok. Moreover, if I run this code in 5.3, I'll get the proper result. I. e. if I use cyrillic, the result message is empty string.

tolec
  • 113
  • 1
  • 8

1 Answers1

0

There are reported issues with non utf-8 chars in exceptions. Try converting the message to utf-8 like so:

throw new Exception(utf8_encode('Сообщение'));

if that does not work then try the following:

$message = 'Сообщение';
$message = mb_convert_encoding($message, 'Windows-1251', 'UTF-8');
throw new Exception($message);

-- EDIT --

The actual problem is not that the exception message is not stored, but rather - the exception is not displayed properly. In PHP 5.3, xdebug is not turned on by default and in PHP 5.4, it is. xdebug is set to display everything in UTF-8 and your message is probably encoded in some other charset, thus the message not being rendered correctly.

If you scroll to the bottom of this page, you will find a single comment referring to this problem.

PHP themselves tracked this issue on here

This stackoverflow thread is also related to the same issue.

You might be able to get away by setting the xdebug encoding to a non utf-8 charset. Please read the xdebug manual regarding this

Community
  • 1
  • 1
dnshio
  • 914
  • 1
  • 8
  • 21
  • The point is that I have already a big project, so unfortunately it's not a solution for me. Probably I'm looking for a setting that will make my code working duly. – tolec Oct 27 '14 at 12:45
  • Please can you give a link to these issues? – tolec Oct 27 '14 at 12:48
  • Edited the answer with some references and a better explanation – dnshio Oct 27 '14 at 13:11
  • killneel, thank you! But does it mean that if I don't use xdebug, I should to add it to php and configure it to use cyrillic charset by default? – tolec Oct 27 '14 at 13:40
  • That could probably help if all your source files are encoded in a cyrillic charset. – dnshio Oct 27 '14 at 13:42
  • Yes, almost all is in cp1251. But there are files in utf. Only this two charsets. – tolec Oct 27 '14 at 13:48
  • And now I'm thinking of converting all of project files to UTF :) – tolec Oct 27 '14 at 13:49
  • ISO-8859-1 cannot represent any Cyrillic characters, so both your `mb` conversion and the equivalent `utf8_encode` *cannot* work. – deceze Oct 27 '14 at 13:50
  • 1
    I've edited the answer to reflect the charset specified by OP. Also, my answer covers content other than charset conversion. Please read the entire thread before downvoting. – dnshio Oct 27 '14 at 14:10