Alex Rivera | Logout

C++ (Standard) Exceptions and Unicode

Asked 2009-03-06T08:56:58.823
10

I'm running into an issue where I'm processing unicode strings and I want to do some error reporting with standard exceptions. The error messages contained in standard exceptions are not unicode.

Usually that hasn't been a problem for me because I can define the error message in non-unicode and have enough information, but in this case I want to include data from the original strings, and these can be unicode.

How do you handle unicode messages in your exceptions? Do you create your own custom exception class, do you derive from the standard exceptions extending them to unicode, or do you have even other solutions to this problem (such as a rule "don't use unicode in exceptions")?

Edit
Report

1 Answer

2

(I'm adding an answer to my own question after an insight because of Flodin's answer)

In my particular case I have a string which may contain unicode characters, which I am parsing and thus expecting to be in a certain format. The parsing may fail and throw an exception to indicate that a problem occurred.
Originally I intended to create a programmer-readable message inside the exception that details the contents of the string where parsing failed, and that's where I ran into trouble because the exception message of a standard exception cannot contain unicode characters.

However, the new design I am considering is to return the location of the parsing error in the string through the exception mechanism within a std::exception-derived class. The process of creating a programmer-readable message that contains the parts of the string causing the error can be delegated to a handler outside the class. This feels like a much cleaner design to me.

Thank you for the input, everyone!

answered 2009-03-06T09:27:01.887

Your Answer