
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Mapping Back and Forth Between Managed Exceptions and HRESULTs
|
389
Discussion
Handling exceptions generated by COM/COM+ components involves the following
two steps:
1. Handle any specific exceptions to which the .NET Common Language Runtime
maps the COM/COM+
HRESULTs you’re interested in. The table in the Solution
section lists the managed exceptions and the standard
HRESULT values returned
by COM/COM+ objects to which they are mapped.
2. Handle any user-defined exceptions that are unique to a specific COM/COM+
component by trapping the
COMException exception. The COMException class
reflects COM/COM+
HRESULTs that have no mapping to managed exceptions.
The following code fragment illustrates this handling of COM/COM+ exceptions:
try
{
CallCOMMethod( );
}
catch (UnauthorizedAccessException uae)
{
// Handle COM/COM+ access exceptions here.
}
catch (System.Runtime.InteropServices.COMException ce)
{
// Handle user-defined COM/COM+ exceptions here.
}
SynchronizationLockException COR_E_SYNCHRONIZATIONLOCK (0x80131518)
SystemException COR_E_SYSTEM (0x80131501)
TargetException COR_E_TARGET
TargetInvocationException COR_E_TARGETINVOCATION (0x80131604)
TargetParameterCountException COR_E_TARGETPARAMCOUNT (0x80138002)
ThreadAbortException COR_E_THREADABORTED (0x80131530)
ThreadInterruptedException COR_E_THREADINTERRUPTED ...