locked
How to use COMException and HResult to

    Question


  • To throw a (Windows Runtime) exception across the ABI boundary, I am directed to either use one of these:
     OutOfMemoryException, InvalidArgumentException, InvalidArgumentException,
     InvalidCastException, NullReferenceException, NotImplementedException,
     AccessDeniedException, FailureException, OutOfBoundsException

    or "To throw a custom exception, you can use COMException and supply a user-defined HRESULT" in the constructor.

    My questions are:
    1) What happens if a C++ exception is left unhandled in a library?

    2) Is the guidance to use exceptions with HResult values or force the user of a library to check each return code?

    3) Must the HResult value be the return from a COM method call? Or can it be a user defined integer?

    4) If they can be user defined, how does the user of two libraries tell which library had the exception?


    References:
    Platform::COMException Class  http://msdn.microsoft.com/en-us/library/windows/apps/hh710414(v=vs.110).aspx
    Exceptions (Visual C++ for Windows Runtime)  http://msdn.microsoft.com/en-us/library/windows/apps/hh441561(v=vs.110).aspx
    C++ Exception Handling  http://msdn.microsoft.com/en-us/library/4t3saedz(v=vs.110).aspx  

    Sunday, July 1, 2012 1:56 AM

Answers

  • Andrew7Webb wrote:

    My questions are:
    1) What happens if a C++ exception is left unhandled in a library?

    From what I have seen the behavior is the same of a desktop app. If the exception bounces back to the runtime library default handler, the process will terminate.

    2) Is the guidance to use exceptions with HResult values or force the user of a library to check each return code?

    Windows does not use exceptions but only hresults (at the very low level, it's COM, so this is expected). This means that WinRT does not understand exceptions.
    If you use C++/CX, use exceptions and the conversion to hresults is automatic. In this case you should catch for possible exceptions.
    If you use WRL you have to use hresults only. In this case you should check for return code.
     > *3) Must the HResult value be the return from a COM method call? Or can it be

    a user defined integer?*

    HRESULT has a specific format, is not a generic int. Look at the msdn documentation to see its definition and the macro used to build or examine its content. For example SUCCESS(hr), and FAILED(hr) are the macros to test if the result is good or bad and this is done examining a specific bit of that integer.

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms679692(v=vs.85).aspx


    4) If they can be user defined, how does the user of two libraries tell which library had the exception?

    HRESULT can be defined and the documentation tells you how can you build it.


    Raffaele Rialdi  http://www.iamraf.net
    Weblog: http://blogs.ugidotnet.org/raffaele
    Microsoft MVP profile https://mvp.support.microsoft.com/profile/raffaele
    UGIdotNET - http://www.ugidotnet.org/


    Raffaele Rialdi [MVP] My articles and videos: http://www.iamraf.net Italian blog: http://blogs.ugidotnet.org/raffaele
    • Marked as answer by Andrew7Webb Sunday, July 1, 2012 6:16 PM
    Sunday, July 1, 2012 10:55 AM

All replies

  • Andrew7Webb wrote:

    My questions are:
    1) What happens if a C++ exception is left unhandled in a library?

    From what I have seen the behavior is the same of a desktop app. If the exception bounces back to the runtime library default handler, the process will terminate.

    2) Is the guidance to use exceptions with HResult values or force the user of a library to check each return code?

    Windows does not use exceptions but only hresults (at the very low level, it's COM, so this is expected). This means that WinRT does not understand exceptions.
    If you use C++/CX, use exceptions and the conversion to hresults is automatic. In this case you should catch for possible exceptions.
    If you use WRL you have to use hresults only. In this case you should check for return code.
     > *3) Must the HResult value be the return from a COM method call? Or can it be

    a user defined integer?*

    HRESULT has a specific format, is not a generic int. Look at the msdn documentation to see its definition and the macro used to build or examine its content. For example SUCCESS(hr), and FAILED(hr) are the macros to test if the result is good or bad and this is done examining a specific bit of that integer.

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms679692(v=vs.85).aspx


    4) If they can be user defined, how does the user of two libraries tell which library had the exception?

    HRESULT can be defined and the documentation tells you how can you build it.


    Raffaele Rialdi  http://www.iamraf.net
    Weblog: http://blogs.ugidotnet.org/raffaele
    Microsoft MVP profile https://mvp.support.microsoft.com/profile/raffaele
    UGIdotNET - http://www.ugidotnet.org/


    Raffaele Rialdi [MVP] My articles and videos: http://www.iamraf.net Italian blog: http://blogs.ugidotnet.org/raffaele
    • Marked as answer by Andrew7Webb Sunday, July 1, 2012 6:16 PM
    Sunday, July 1, 2012 10:55 AM
  • Thank you.  From your link, I see I can create a HRESULT with

     int hresult = MAKE_HRESULT( SEVERITY_ERROR, FACILITY_ITF, unsigned short errcode );

    with the definitions in WinError.h


    • Edited by Andrew7Webb Sunday, July 1, 2012 6:29 PM give credit.
    Sunday, July 1, 2012 3:55 PM
  • Andrew7Webb wrote:

    Thank you.   I see I can create a HRESULT with

     inthresult = MAKE_HRESULT( SEVERITY_ERROR, FACILITY_ITF, unsigned short errcode );

    with the definitions in WinError.h

    exactly. I pointed you to the docs as there are many other useful macros you may want to use.


    Raffaele Rialdi  http://www.iamraf.net
    Weblog: http://blogs.ugidotnet.org/raffaele
    Microsoft MVP profile https://mvp.support.microsoft.com/profile/raffaele
    UGIdotNET - http://www.ugidotnet.org/


    Raffaele Rialdi [MVP] My articles and videos: http://www.iamraf.net Italian blog: http://blogs.ugidotnet.org/raffaele
    Sunday, July 1, 2012 5:20 PM