Convert HRESULT return value to Exception


  • I vaguely remember reading about a function that would take an HRESULT and throw the proper exception if !SUCCESS().  Does this function already exist or do I remember incorrectly?  If it doesn't exist then it would be easy to implement, but I would rather not rewrite it if not necessary.
    Saturday, March 23, 2013 7:17 PM


All replies

  • I had adventures traveling the HRESULT route as the available documentation (reference the following message thread) does not readily support C++/CX.  I documented my adventures as well as blogged on the topic - the following thread should help you along your way as it will outline unsupported features:


    Note: the blog referenced in the link above has source code.

    MCAD.NET C# - http://www.Global-webnet.com/Blog

    • Proposed as answer by Jesse Jiang Monday, March 25, 2013 2:53 AM
    • Marked as answer by Jesse Jiang Friday, March 29, 2013 2:11 AM
    Sunday, March 24, 2013 1:56 PM
  • I get a 500 - Internal server error when I try to go to the blog post.

    My understanding is that when an non SUCCESS HRESULT is returned from a component it is packaged automagically by C++/CX and thrown as an exception deriving from Platform::Exception.  What I want to be able to do is create a function that does the same thing that I can pass an HRESULT to.  I vaguely recall running across the header file where the C++/CX runtime does this, and I was wondering if there was a function that I could explicitly call to do this.

    Wednesday, March 27, 2013 7:00 PM
  • Hmmmm, looks like someone hacked my site - I resolved the issue and it is available now.   

    My blog discusses "a way" to throw a user defined HRESULT.  

    The following link suggest that you can specify an hresult in the Exception constructor parameter, e.g., throw new ref Exception(hresult[, String^ message])



    The following suggest that we can easily throw "Standard exceptions" via C++/CX.



    MCAD.NET C# - http://www.Global-webnet.com/Blog

    Wednesday, March 27, 2013 11:04 PM