大网数据广告活动
网络与域名解析

Indicate Errors by Exceptions

· 52CE 我爱测

For traditional C programmers, errors are commonly returned through return values, or a special \ out\ parameter that returns the error code.

For traditional C programmers, errors are commonly returned through return values, or a special [out] parameter that returns the error code. This leads to interfaces implemented the following way:

long sample(...)
{
    ...
    p = new Cbar(...);
    if (p == NULL)
    {
        // cleanup
        ...
        return ERROR_OUTOFMEMORY;
    }
}

The problem with this approach is that RPC return values are simply long integers. They have no special meaning as errors (note that error_status_t has no special semantics on the server side), which carries important implications.

First, RPC is not alerted that the operation failed; it attempts to unmarshal all [in, out] and [out] arguments. The failure semantics of context handles are also different. The packet returned to the client is essentially a success packet, with the error code buried deep in the packet. This also means RPC does not use extended error information, so client software often is unable to discern where the call failed.

Indicating errors in RPC server routines by throwing Structured Exception Handling (SEH) exceptions (not C++) is a much better approach. When an SEH exception is thrown, control goes directly to the RPC run time. An error sometimes occurs deep in a routine that cannot properly clean up, and it needs to indicate an error to its caller. The routine should return an error to its caller, which in turn can return an error to its caller and so on. However, the last server routine on the stack should throw an exception before it returns to RPC to indicate to RPC that an error occurred.

Related topics

Exception Handling

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。