Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to throw exception in VIsual C++ using a wchar_t string?

I have legacy code which I am incrementally porting to Unicode characters in Visual C++ (wchar_t). I've encountered this bit of code that I'd like to convert:

char tmp[256];
sprintf(tmp, "stuff");
throw exception(tmp);

I want to change it to something like this (this gives me a compile error on exception):

wchar_t tmp[256];
swprintf(tmp, "stuff");
throw exception(tmp);

So far I haven't found any document to give me the Unicode equivalent for throw exception, can anyone help me?

Of course I could convert my "tmp" back into a char string, but that just seems silly to have to do that.

like image 660
Alan Moore Avatar asked Sep 17 '25 21:09

Alan Moore


1 Answers

std::exception does not support wchar_t strings, so you will have to either convert your wchar_t buffer into a separate char buffer, or do not switch to a wchar_t buffer to begin with as sprintf() does support formatting Unicode input via its %S and %ls formatting specifiers, eg:

char tmp[256]; 
sprintf(tmp, "%ls", wchar_t data here); 
throw exception(tmp); 
like image 133
Remy Lebeau Avatar answered Sep 21 '25 17:09

Remy Lebeau