How to throw exception
-
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
-
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
-
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.
-
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.
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
-
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