locked
Custom exceptions

    Question

  • 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.

     

    Sunday, November 18, 2012 2:53 PM

All replies

  • Hi,

    There is no way to customer the exception thrown from the Windows Runtime.

    You can throw any exception type that's defined by the Windows Runtime. You cannot derive custom types from any Windows Runtime exception type. However, you can throw COMException and provide a custom HRESULT that can be accessed by the code that catches the exception. There's no way to specify a custom Message in a COMException.

    Please check http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh441569.aspx

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, November 22, 2012 8:18 AM
  • Hello,

    Thanks for that. I had looked at quite a bit of documentation, but I hadn't seen your link, where it explicitly states that custom messages can't be used.

    I have one other question relating to this. It states here http://msdn.microsoft.com/en-us/library/windows/desktop/ms679751 that:

    "All the COM-defined FACILITY_ITF codes have a code value in the range of 0x0000-0x01FF. While it is legal to use any codes in FACILITY_ITF, it is recommended that only code values in the range of 0x0200-0xFFFF be used. This recommendation is made as a means of reducing confusion with any COM-defined errors."

    I did a search on FACILITY_ITF in the visual studio headers, and some system libraries appear to be using codes in the range 0x0200-0xFFFF. Does this mean that if I create a custom code in this range, and catch an exception with that code, I can't be 100% sure it's my code?



    • Edited by RobertHF Thursday, November 22, 2012 3:04 PM typo
    Thursday, November 22, 2012 3:03 PM