Handling Unknown Errors

It is legal to return a status code only from the implementation of an interface method sanctioned as legally returnable. Failure to observe this rule invites the possibility of conflict between returned error-code values and those sanctioned by the application. Pay particular attention to this potential problem when propagating error codes from functions that are called internally.

Applications that call interfaces should treat any unknown returned error code (as opposed to a success code) as synonymous with E_UNEXPECTED. This practice of handling unknown error codes is required by clients of the COM-defined interfaces and functions. Because typical programming practice is to handle a few specific error codes in detail and treat the rest generically, this requirement of handling unexpected or unknown error codes is easily met.

It is important to handle all possible errors when calling an interface method. Failure to do so could cause your application to crash, to corrupt data, or to become vulnerable to security exploits. The following code sample shows the recommended way of handling unknown errors:

hr = xxMethod(); 
switch (GetScode(hr))  
    case NOERROR: 
      // Method returned success. 
    case x1: 
      // Handle error x1 here.
    case x2: 
      // Handle error x2 here.
    case E_UNEXPECTED: 
      // Handle unexpected errors here. 

The following error check is often used with those routines that do not return anything special (other than S_OK or some unexpected error):

if (xxMethod() == NOERROR) 
    // Handle success here.
    // Handle failure here.

Related topics

Error Handling in COM