From: Timothy Madden on
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.

Having all other functions throughout my application catch my specific
exception type, next to and separately from std::exception, would be a poor
solution I think.

Thank you,
Timothy Madden


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On Jul 17, 11:28 pm, Timothy Madden <terminato...(a)gmail.com> wrote:
> Can I somehow create an exception class derived from std::exception

Yes.


> I mean the message returned std::exception::what() is assumed to be in the
> application locale

I don't think much is assumed about that.


> and I can not just set the application locale to UTF-8.

You could convert your UTF-8 to the right charset. Only somewhat
portable way I can think of is to convert it to wide characters, since
those are either UTF-16 or UTF-32, then convert that to the local
charset with the locale's codecvt<wchar_t, char, mbstate_t>.



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Goran on
On Jul 18, 12:28 am, Timothy Madden <terminato...(a)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! ]

From: tf on
Timothy Madden wrote:
> 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 ?

Don't worry too much about the what() message. It's nice to have a
message that a programmer stands a chance of figuring out, but you're
very unlikely to be able to compose a relevant and user-comprehensible
error message at the point an exception is thrown. Certainly,
internationalization is beyond the scope of the exception class
author. Peter Dimov makes an excellent argument that the proper use
of what() string is to serve as a key into a table of error message
formatters. Now if only we could get standardized what() strings for
exceptions thrown by the standard library...

-- http://www.boost.org/community/error_handling.html

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Timothy Madden on
Mathias Gaunard wrote:
> On Jul 17, 11:28 pm, Timothy Madden <terminato...(a)gmail.com> wrote:
>> Can I somehow create an exception class derived from std::exception
>
> Yes.
>
>
>> I mean the message returned std::exception::what() is assumed to be in the
>> application locale
>
> I don't think much is assumed about that.
>
>
>> and I can not just set the application locale to UTF-8.
>
> You could convert your UTF-8 to the right charset. Only somewhat
> portable way I can think of is to convert it to wide characters, since
> those are either UTF-16 or UTF-32, then convert that to the local
> charset with the locale's codecvt<wchar_t, char, mbstate_t>.

A C++ application has two character sets: the /execution character set/
and the /execution wide-character set/ (and a few others, which are
sub-sets of these, and are referred in the standard).

My problem is the "right charset", as you call it, would be the
/execution wide-character set/, whereas std::exception::what() only
returns a narrow string. Converting my UTF-8 messages to narrow strings
in the application locale would loose extended characters (messages are
in Korean, and the application locale might be different than Korean).

So the point is I would need to carry and output the message as a wide
string, which std::exception lacks.

I find this narrow what() string a limitation in the C++ standard and I
am surprised the problem has not been addressed yet.

Thank you,
Timothy Madden

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]