
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
390
|
Chapter 7: Exception Handling
catch (Exception e)
{
// Handle all other exceptions here.
}
See Recipe 7.9 for more information on handling user-defined HRESULTs.
See Also
See Recipe 7.9; see the “Error Raising and Handling Guidelines,” “HRESULTs and
Exceptions,” and “Handling COM Interop Exceptions” topics in the MSDN
documentation.
7.9 Handling User-Defined HRESULTs
Problem
A COM object can return a user-defined HRESULT or an HRESULT that has no mapping
to a managed exception type. You wish to handle these returned
HRESULTs in a more
specific manner.
Solution
The following code fragment illustrates the handling of user-defined COM/COM+
exceptions:
try
{
CallCOMMethod( );
}
catch (System.Runtime.InteropServices.COMException ce)
{
switch ((uint)ce.ErrorCode)
{
case 0x80042000:
// Handle specific user-defined COM/COM+ exception here.
break;
case 0x80042001:
// Handle specific user-defined COM/COM+ exception here.
break;
default:
// Handle any other specific user-defined COM/COM+
// exceptions here.
break;
}
}
catch (Exception e)
{
// Handle all other exceptions here.
}