How can I get wstring_convert::to_bytes to throw a range_error exception?

Class Skeleton

I am using std::wstring_convert to convert a wstring into a multibyte string as follows:

    // convert from wide char to multibyte char
    try
    {
        return std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(wideMessage);
    }

    // thrown by std::wstring_convert.to_bytes() for bad conversions
    catch (std::range_error& exception)
    {
        // do something...
    }

In order to unit test the block I have commented as do something... I wish to pass a wstring that will throw a std::range_error exception.

However, I have not been able to formulate such a wstring that will fail such a conversion. The wstring will use UTF16 and I have been reading about high and low surrogates. For example, a UTF16 character of D800 followed by "b" should be invalid. std::wstring(L"\xd800b"); fails to compile on the same grounds possibly. If I create a wstring such as below it will not throw the exception on conversion:

std::wstring wideMessage(L" b");
wideMessage[0] = L'\xd800';

// doesn't throw
std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(wideMessage);

Is there a suitable wstring I can use to throw an exception during the conversion?

I have tried 5.1, 5.2 and 5.3 from this link. I am using Visual Studio 2015.

一二三

Microsoft's implementation of std::codecvt_utf8 appears to successfully convert any UTF-16 code unit into UTF-8—including surrogate pairs. This is a bug, as surrogates are not encodable. Both libc++ (LLVM) and libstdc++ (GCC) will correctly throw a std::range_error and fail to convert unpaired surrogates.

Looking at their code, it appears that the only way for it to throw is if a character is greater than the Maxcode template parameter of the facet. For example:

std::wstring_convert<std::codecvt_utf8<wchar_t, 0x1>>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is wstring_convert throwing a range_error?

From Java

How can I manually return or throw a validation error/exception in Laravel?

From Java

How can I get a JavaScript stack trace when I throw an exception?

From Dev

How can I get a method to throw a custom exception I've created?

From Dev

How can I get a method to throw a custom exception I've created?

From Dev

How can I convert a rejected promise to an exception and throw it from an Express route handler?

From Dev

How can I throw a 403 exception in Symfony2?

From Dev

How can I return None or throw an exception if no implicit defined?

From Dev

How can I mock a void method to throw an exception?

From Dev

How can I create a new exception class and throw it

From Dev

How can i find why RestSharp PUT throw Exception?

From Dev

How can i throw exception if processing don't response

From Dev

How can I make an incorrect pointcut expression throw an exception?

From Dev

How can I define a Stream with a "throw exception" value?

From Dev

How can I throw exception inside a Generic Type Method?

From Dev

How can i throw exception if processing don't response

From Dev

how can I mock a service to throw an exception a method that returns a List?

From Dev

How do I get MySQL to throw a conditional runtime exception in SQL

From Dev

How can I throw error in onNext() with RxJava

From Dev

(Why) Can I not throw an exception out of a generator?

From Dev

Can I catch a throw exception from method?

From Dev

In Rails, can I throw an exception but keep going?

From Dev

Can I throw exception with java bean

From Dev

In Java, can "throw exception" replace "return" statement? Where should I put the return statement to get a return value?

From Dev

How can I throw an exception in a Java web project that can't be caught and which ends execution?

From Dev

How to know if some method can throw an exception

From Dev

How do I throw Exception again and catch it

From Dev

Inno Setup: How can I get the Exception Code of a raised Exception?

From Dev

Throw exception when get error on reflection

Related Related

  1. 1

    Why is wstring_convert throwing a range_error?

  2. 2

    How can I manually return or throw a validation error/exception in Laravel?

  3. 3

    How can I get a JavaScript stack trace when I throw an exception?

  4. 4

    How can I get a method to throw a custom exception I've created?

  5. 5

    How can I get a method to throw a custom exception I've created?

  6. 6

    How can I convert a rejected promise to an exception and throw it from an Express route handler?

  7. 7

    How can I throw a 403 exception in Symfony2?

  8. 8

    How can I return None or throw an exception if no implicit defined?

  9. 9

    How can I mock a void method to throw an exception?

  10. 10

    How can I create a new exception class and throw it

  11. 11

    How can i find why RestSharp PUT throw Exception?

  12. 12

    How can i throw exception if processing don't response

  13. 13

    How can I make an incorrect pointcut expression throw an exception?

  14. 14

    How can I define a Stream with a "throw exception" value?

  15. 15

    How can I throw exception inside a Generic Type Method?

  16. 16

    How can i throw exception if processing don't response

  17. 17

    how can I mock a service to throw an exception a method that returns a List?

  18. 18

    How do I get MySQL to throw a conditional runtime exception in SQL

  19. 19

    How can I throw error in onNext() with RxJava

  20. 20

    (Why) Can I not throw an exception out of a generator?

  21. 21

    Can I catch a throw exception from method?

  22. 22

    In Rails, can I throw an exception but keep going?

  23. 23

    Can I throw exception with java bean

  24. 24

    In Java, can "throw exception" replace "return" statement? Where should I put the return statement to get a return value?

  25. 25

    How can I throw an exception in a Java web project that can't be caught and which ends execution?

  26. 26

    How to know if some method can throw an exception

  27. 27

    How do I throw Exception again and catch it

  28. 28

    Inno Setup: How can I get the Exception Code of a raised Exception?

  29. 29

    Throw exception when get error on reflection

HotTag

Archive