This article supporting source code
First, the preface
In programming, error handling is essential and usually takes up a lot of space. This read the method of handling errors (exceptions) that are landed in COM.
In a component program, if you encounter an error, there are generally two ways to handle it.
Second, simple return
For simpler errors, return directly to the HRESULT that represents the cause of the error. For example, here are some common error values:
E_invalidarg |
0x80070057 |
Parameter error |
E_outofmemory |
0x8007000e |
Memory error |
E_NotImpl |
0x80004001 |
Not implemented |
E_pointer |
0x80004003 |
Invalid pointer |
E_handle |
0x80070006 |
Invalid handle |
E_abort |
0x80004004 |
Terminate operation |
E_accessdenied |
0x80070005 |
Access Denied |
E_nointerface |
0x80004002 |
Interface not supported |
Alternatively, you can return to construct an HRESULT error value yourself. method is to use the macro Make_hresult (Sev,fac,code)
Parameters |
Meaning |
Value (binary) |
SEv Severity |
Success |
00 |
Successful but there are 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 |
Code unique error code |
16 bits (bit) you define yourself. |
|
Once the caller gets the HRESULT value returned, you can also use the macro hresult_severity (), hresult_facility (), Hresult_code () to obtain SEV error levels, FAC device information, and code error codes.