Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. How to throw exception

How to throw exception

Scheduled Pinned Locked Moved COM
comtutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tpndtbk
    wrote on last edited by
    #1

    Hi, Does anyone know how to throw exception in COM. Thanks in advance

    T S M 3 Replies Last reply
    0
    • T tpndtbk

      Hi, Does anyone know how to throw exception in COM. Thanks in advance

      T Offline
      T Offline
      ThatsAlok
      wrote on last edited by
      #2

      tpndtbk wrote: Does anyone know how to throw exception in COM. Till i know COM doen't support any Exception handlling. but there other way or hack to do that ----------------------------- "I Think It Will Help" ----------------------------- Alok Gupta visit me at http://www.thisisalok.tk

      T 1 Reply Last reply
      0
      • T ThatsAlok

        tpndtbk wrote: Does anyone know how to throw exception in COM. Till i know COM doen't support any Exception handlling. but there other way or hack to do that ----------------------------- "I Think It Will Help" ----------------------------- Alok Gupta visit me at http://www.thisisalok.tk

        T Offline
        T Offline
        tpndtbk
        wrote on last edited by
        #3

        Hi Alok, You mean we can't throw exception in a COm interface function? So what can we do if the exception happens??

        1 Reply Last reply
        0
        • T tpndtbk

          Hi, Does anyone know how to throw exception in COM. Thanks in advance

          S Offline
          S Offline
          sashishk
          wrote on last edited by
          #4

          Implement ISupportErrorInfo interface, and always try to get the HRESULT code. HRESULT tells whether the method executed successfully or some error occured. I think this might be of your help.

          T 1 Reply Last reply
          0
          • S sashishk

            Implement ISupportErrorInfo interface, and always try to get the HRESULT code. HRESULT tells whether the method executed successfully or some error occured. I think this might be of your help.

            T Offline
            T Offline
            tpndtbk
            wrote on last edited by
            #5

            Thanks very much. Year, I always use HRESULT code... But I mean, how to throw an error message that the user can catch it and see it.

            T 1 Reply Last reply
            0
            • T tpndtbk

              Thanks very much. Year, I always use HRESULT code... But I mean, how to throw an error message that the user can catch it and see it.

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              tpndtbk wrote: I always use HRESULT code... But I mean, how to throw an error message that the user can catch it and see it. Sorry for replying late, Actually when your class inherited From ISupportInterface. it has a special function name Error which help you to send special human readable warning message or failure reason to client which using your component ----------------------------- "I Think this Will Help" ----------------------------- Alok Gupta visit me at http://www.thisisalok.tk

              1 Reply Last reply
              0
              • T tpndtbk

                Hi, Does anyone know how to throw exception in COM. Thanks in advance

                M Offline
                M Offline
                Manuele Sicuteri
                wrote on last edited by
                #7

                Hi. If, for example, you want to throw an exception from a certain method "Test()" of a certain COM interface "IMyInterface", a possible simple solution is proposed below. You can use either the macro "_com_raise_error(...)" or the command "throw _com_error(...)" inside the method "Test()" for throwing an exception. In the "client" of your "Test()" method, you can catch this exception as a "_com_error". In the following you can find a code example. Hoping this could help, Manuele Server ====== #include "comdef.h" ... // -------------------------------------------------------------------------- STDMETHODIMP CMyInterface::Test( int p_Int ) { HRESULT l_HR = S_OK; // Let's suppose we want to throw an exception if the // parameter is zero. if ( p_Int == 0 ) { // Create a new error info object and fill it. ICreateErrorInfo* l_CreateErrorInfo = NULL; GUID l_GUID = CLSID_MyInterface; l_HR = CreateErrorInfo( &l_CreateErrorInfo ); if ( ! SUCCEEDED( l_HR ) ) { return E_FAIL; } l_CreateErrorInfo->SetDescription( L"Descrizione Errore" ); l_CreateErrorInfo->SetGUID( l_GUID ); l_CreateErrorInfo->SetHelpContext( 0 ); l_CreateErrorInfo->SetHelpFile( L"HelpFile" ); l_CreateErrorInfo->SetSource( L"Source" ); IErrorInfo* l_ErrorInfo = NULL; l_HR = l_CreateErrorInfo->QueryInterface( IID_IErrorInfo, ( void** ) &l_ErrorInfo ); if ( ! SUCCEEDED( l_HR ) ) { return E_FAIL; } l_CreateErrorInfo->Release(); // Throw the just created error info object. // Alternatively, here you can use // "_com_raise_error( E_FAIL, l_ErrorInfo );" as well. throw _com_error( E_FAIL, l_ErrorInfo ); return E_FAIL; } return S_OK; } Client ====== #import "..." no_namespace // -------------------------------------------------------------------------- int main(int argc, char* argv[]) { HRESULT l_HR = S_OK; IMyInterfacePtr l_MyPtr = NULL; l_HR = CoInitialize( NULL ; if ( ! SUCCEEDED( l_HR ) ) { return E_FAIL; } l_HR = l_MyPtr.CreateInstance( __uuidof(MyInterface) ); if ( ! SUCCEEDED( l_HR ) ) { return E_FAIL; } try { l_HR = l_MyPtr->Test( 0 ); } catch( _com_error e ) { // Here, you can get information made available // by interface IErrorInfo _bstr_t l_Description = e.Description(); DWORD l_HelpContext = e.HelpContext(); _bstr_t l_HelpFile = e.HelpFile(); _bstr_t l_Source = e.Source(); GUID

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups