Re: UTF-8 messages in exceptions ?
On Jul 18, 12:28 am, Timothy Madden <terminato...@gmail.com> wrote:
Hello
I need to write some wrapper classes around a library that my client has,
and the error messages (and all the other strings in the library) are in
UTF-8. Can I somehow create an exception class derived from std::exception
(std::runtime_error) that could carry such messages ?
I mean the message returned std::exception::what() is assumed to be in the
application locale, and I can not just set the application locale to UTF-8.
If standard library and other librarries you use aren't localized,
then they are most likely in English, and that's OK for plain UTF-8.
So when you output what() to something UTF-8 aware, it's OK.
If they are localized, and are using specific locale (not UTF-8),
whoops! How about some simple mix-in derivation, e.g.:
class utf8_error
{
virtual const char* what_utf8() const = 0;
}
then,
class my_error : public runtime_error, public utf8_error
{
// Implement what and what_utf8
};
and finally, in you catch handlers, use:
string utf8_ed_what(const exception& e)
{
const utf8_error* utf8 = dynamic_cast<const utf8_error*>(&e);
if (utf8)
return utf8->what_utf8();
else
return locale_text_to_utf8(e.what());
}
BTW, application locale is assumed? How? (Honest question).
Goran.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]