My code consists of a C++/XAML application calling a WinRT component, also written in C++. I'd like to be able to raise an exception within the WinRT component, give it a message string of my choosing, catch it in the main application, and display the message.
I am catching the exceptions ok, but my problem is getting any sort of message out of the caught exception.
I started trying to use COMException. This takes an HRESULT and a message string in the constructor, so I tried (The 0xFFF-1 is a hopeful stab at a valid code):
COMException^ ex =
refnewCOMException(MAKE_HRESULT(SEVERITY_ERROR,
FACILITY_ITF, 0xFFFF-1),
refnewString(L"test"));
However, when I catch the exception and do:
String^ str = ex -> Message;
I get a null string. This also happens if I check the Message property immediately after creating the exception, before throwing it.
I then searched the samples for any code throwing an exception, and I found code throwing a FailureException and passing it a string in the constructor, but no code catching it. I then tried it for myself, again passing in "test", but when I looked
at the message, it was "Unspecified error".
Debugging into the Exception::Message property, it calls ::FormatMessageW() with FORMAT_MESSAGE_FROM_SYSTEM as the first argument, so it only seems to work if the exception uses one of the standard messages.
Is there a way to get a custom message across in an exception?
Thank for any advice.