ATL server throw exception 
Author Message
 ATL server throw exception

In Visual C++ 6, I select  ATL COM AppWizard to create a new project.
I added a simple com class, CMycomclass,  with 1 method.

[id(1), helpstring("method foo")] HRESULT foo([in] long id, [out,
retval] long *pVal);

Here is the implementation of foo:

STDMETHODIMP CMycomclass::foo( long id, long* pVal)
{
  IDBInitialize*  pIDBInitialize = NULL;
try {
  HRESULT         hr = ::CoCreateInstance(CLSID_OraOLEDB, NULL,
CLSCTX_INPROC_SERVER,  IID_IDBInitialize, (void**)ppIDBInitialize);

Quote:
}

catch (_com_error& e)
{
   hr = e.Error();
Quote:
}

catch (...)
{
  hr = E_FAIL;
Quote:
}

*pVal = hr;
return hr;  

Quote:
}

There is no real purpose to this ATL COM server. It does try to
instantiate the Oracle Provider for OLEDB. But doesn't try to actually
connect or anything useful. It just has the one call to
CoCreateInstance. I purposefully ran this on a PC where the Oracle
Provider WAS NOT installed, so the CoCreateInstance should fail.

I then created a simple VB project to call this ATL server and call
this method, and "watch" it fail:

Sub MyVBSub( )
On Error GoTo err_handler
Dim z As MyATLSERVERLib.MyComClass
Set z = New MyATLSERVERLib.MyComClass
Dim retval As Long
Dim id As Long
id = 1
retval = 0
retval = z.foo(id)

...
err_handler:
....
End Sub

When I step through the project , why does this call z.foo throw an
exception .  The call to foo causes a jump to the err_handler, the
error being 429 ActiveX can't create component.
That is EXPECTED because the Oracle Provider is not installed and so
when the ATL COM Server method foo attempts to call CoCreateInstance
it fails, That is all as it should be, But why does it cause the VB
error handler to be invoked.

 I use try-catch inside the ATL COM server. Shouldn't that be catching
any exceptions and set retval. So that when VB calls the one method
and the method throws , it nicely sets retval and returns to VB
without triggering the VB exception handling.????

cbb



Sun, 06 Nov 2005 06:30:47 GMT  
 ATL server throw exception

Quote:
> That is all as it should be, But why does it cause the VB
> error handler to be invoked.

CoCreateInstance fails, and you are returning the failed HRESULT. This is
what VB sees, and why it is handling the response as an error.

Quote:
>  I use try-catch inside the ATL COM server. Shouldn't that be catching
> any exceptions and set retval.

Only if an exception is thrown. There is no exception thrown by
CoCreateInstance(). It is simply returning an HRESULT.

 So that when VB calls the one method

Quote:
> and the method throws , it nicely sets retval and returns to VB
> without triggering the VB exception handling.????

In VB, you can use: On Error Resume Next, and then test the Err object
manually for any failed HRESULTS, if you so desire.


Sun, 06 Nov 2005 08:13:25 GMT  
 ATL server throw exception
Brian-
Thank you. I have never seen that behavior of VB explained clearly. My
intention was to PREVENT the VB client "throwing" an error. But still
have the ATL server be able to send back a code that would indicate
the problem. In particular , the ATL server is used to connect to a
database using the Oracle Provider and so that is why it calls
CoCreateInstance because it's trying to instantialte that Oracle COM
ojbect. But I wanted to be able to handle the situation if the Oracle
Provider might not be installed, (or installed correctly). Then of
course CoCreateInstance will fail. But if you return the HRESULT back
out to VB, VB puts up error 429 ActiveX component can't create object
, which I thought might be MISINTERPRETED by some to thiink that the
actual ATL COM server wasnot installed correctly.  So I was looking to
prevent that but still return info about the exact location of the
problem.

Anyway, thanks for the answer.

cbb

On Tue, 20 May 2003 17:13:25 -0700, "Brian Muth"

Quote:

>> That is all as it should be, But why does it cause the VB
>> error handler to be invoked.

>CoCreateInstance fails, and you are returning the failed HRESULT. This is
>what VB sees, and why it is handling the response as an error.

>>  I use try-catch inside the ATL COM server. Shouldn't that be catching
>> any exceptions and set retval.

>Only if an exception is thrown. There is no exception thrown by
>CoCreateInstance(). It is simply returning an HRESULT.

> So that when VB calls the one method
>> and the method throws , it nicely sets retval and returns to VB
>> without triggering the VB exception handling.????

>In VB, you can use: On Error Resume Next, and then test the Err object
>manually for any failed HRESULTS, if you so desire.



Sun, 06 Nov 2005 22:40:27 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Error : Server threw an exception

2. ATL COM DLL :: DllRegisterserver throw exception 0x80029c4a

3. Throw exception from ATL COM for both VB and VC++ client

4. Throwing exceptions in ATL dll with optimize for SPEED causes Access Violation

5. ATL File->Print in browser throws exception

6. Accessing ATL-Exe Server throws E_ACCESSDENIED ?!

7. Exception handling - how to know which exceptions are thrown

8. First chance exception in ATL server.

9. property throws Exception in visual designer!

10. Throwing Exceptions in Constructors

11. System.Drawing.SafeNativeMethods threw an exception

12. std::list.clear() throws exceptions in .NET 2003.

 

 
Powered by phpBB® Forum Software