COM Component Design and Application (12) -- error and Exception Handling

Source: Internet
Author: User

I. Preface

In programming, error handling is essential and usually takes a lot of space. This return describes how to handle errors (Exceptions) in COM.

In a component program, if an error occurs, there are two methods to handle it.

Ii. Simple return

For a relatively simple error, the hresult indicating the cause of the error is directly returned. For example, the following are common error values:

  

E_invalidarg Zero X 80070057 Parameter Error
E_outofmemory 0x8007000e Memory Error
E_notimpl Zero X 80004001 Not Implemented
E_pointer Zero X 80004003 Invalid Pointer
E_handle Zero X 80070006 Invalid handle
E_abort Zero X 80004004 Terminate operation
E_accessdenied Zero X 80070005 Access denied
E_nointerface Zero X 80004002 Unsupported Interface

In addition, you can return the self-constructed hresult error value. The macro make_hresult (SEV, FAC, Code) is used)

  

Parameters Description Value (Binary)

Sev severity

Successful 00
Succeeded, but some report information 01
Warning 10
Error 11

FAC device information

Facility_aaf 00000010010
Facility_acs 00000010100
Facility_backgroundcopy 00000100000
Facility_cert 00000001011
Facility_complus 00000010001
Facility_configuration 00000100001
Facility_control 00000001010
Facility_dispatch 00000000010
Facility_dplay 00000010101
Facility_http 00000011001
Facility_internet 00000001100
Facility_itf 00000000100
Facility_mediaserver 00000001101
Facility_msmq 00000001110
Facility_null 00000000000
Facility_rpc 00000000001
Facility_scard 00000010000
Facility_security 00000001001
Facility_setupapi 00000001111
Facility_sspi 00000001001
Facility_storage 00000000011
Facility_sxs 00000010111
Facility_umi 00000010110
Facility_urt 00000010011
Facility_win32 00000000111
Facility_windows 00000001000
Facility_windows_ce 00000011000

Unique error code

You can define 16 bits.  

After obtaining the returned hresult value, the caller can also use the macro hresult_severity (), hresult_facility (), hresult_code () to obtain the sev error severity, FAC device information, and code error code.

Iii. error message interface

Since com relies on a variety of interfaces to provide services, it is natural to think that there is an interface that can provide richer error information reports? The answer is: isupporterrorinfo. The following code is a general method for using isupporterrorinfo:

Double-click all code
1234567891011121314151617 STDMETHODIMP Cxxx::fun(){  ... ... ... ...  CComQIPtr<
ICreateErrorInfo> spCEI;  ::CreateErrorInfo( &spCEI );Spcei-> setguid (iid_ixxx); // The IID of the interface with an error      spCEI->SetSource( L"xxx.xxx" );  // ProgID// If your component provides a Help file at the same time, you can:Spcei-> sethelpcontext (0); // you can specify the topic number of the Help file.Spcei-> sethelpfile (L "XXX. HLP"); // you can specify the file name of the Help file.Spcei-> setdescription (L "error description ");  CComQIPtr <
IErrorInfo > spErrInfo = spCEI;  if( spErrInfo ): Seterrorinfo (0, sperrinfo); // The caller can get the error message.  return E_FAIL;}

The above is the principle code. In the program we write, you don't have to worry about it. Because Atl has packaged the above Code into six overload functions of ccomcoclass: Error. In this way, we can simply rewrite it:

Double-click all code
12345 STDMETHODIMP Cxxx::fun(){  ... ... ... ...Return Error (L "error description ");}

4. Try/catch

After learning C ++, many people prefer the try/catch Exception Handling structure. If you use the ATL of vc6.0, the compiler does not support exception handling by default. After compilation, it will report "Warning c4530: C ++ exception handler used, but unwind semantics are not enabled. specify-Gx ", the solution is to manually add the compilation switch:

Figure 1. Add the compilation switch to support C ++ Exception Handling Structure

In vc.net 2003, the compiler supports Exception Handling structures by default, so no special settings are required. If you want to reduce the size of the target file, you can also choose not to use C ++ Exception Processing, then in the project attribute

Figure 2. Modify vc.net to check whether the C ++ exception structure compilation switch is supported

5. Client receiving component error messages

1. If you call a component using APIs, the method for receiving errors is:

Double-click all code
1234567891011121314151617181920 Hresult hR = SPXXX-> fun () // call the component FunctionIf (failed (HR) // if an error occurs{  CComQIPtr <
ISupportErrorInfo > Spsei = SPXXX; // does the component provide the isupporterrorinfo interface?If (spsei) // If yes, then  {HR = spsei-> interfacesupportserrorinfo (iid_ixxx); // does the ixxx Interface Support error handling?    if( SUCCEEDED( hr ) ){// Yes. Retrieve error message      CComQIPtr <
IErrorInfo > Sperrinfo; // declare the ierrorinfo InterfaceHR =: geterrorinfo (0, & sperrinfo); // obtain the interface      if( SUCCEEDED( hr ) )      {        CComBSTR bstrDes;Sperrinfo-> getdescription (& bstrdes); // gets the error descriptionOther information can also be obtained.      }    }  }}

2. If you use the # import and other packaging methods to call components, the method for receiving errors is:

Double-click all code
123456789 try{... // Call the component Function}catch( _com_error &e ){E. Description (); // gets the error description... // You can also call the _ com_error function to obtain other information.}

6. Compile component programs that support error handling

It is very simple. You only need to select isupporterrorinfo when adding the ATL component object.

Figure 3. Selected components in vc6.0 support error handling Interfaces

Figure 4. Selected components in vc.net 2003 support error handling Interfaces

VII. Summary

After reading this article, download the sample program. The example program demonstrates three error handling methods and three methods for receiving errors, and the program also has more detailed annotations.

 

Http://tech.ddvip.com/2006-04/11447206804882_2.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.