> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XErrorCallback

> XErrorCallback

# XErrorCallback

如果游戏 OS 或游戏运行时出现错误，则调用的回调。

## Syntax

```cpp theme={null}
bool XErrorCallback(  
         HRESULT hr,  
         const char* msg,  
         void* context  
)  
```

### Parameters

*hr*   \_In\_\
类型：HRESULT

与错误关联的 HRESULT 代码。

*msg*   \_In\_z\_\
类型：char\*

错误的文本描述。

*context*   \_In\_\
类型：void\*

回调的上下文指针。

### Return value

类型：bool

返回 `true` 指示系统应继续执行并按 [XErrorSetOptions](/reference/system/xerror/functions/xerrorsetoptions) 指定的方式操作；否则，返回 `false` 忽略错误。

## Remarks

使用 [XErrorSetCallback](/reference/system/xerror/functions/xerrorsetcallback) 函数在游戏中指定发生错误时系统应调用的 `XErrorCallback` 回调，并使用 `XErrorSetOptions` 函数指定游戏 OS 或游戏运行时出现错误时游戏应如何行为。

下面的示例演示如何设置错误选项、定义自定义错误回调，以及设置自定义错误回调以接收系统的错误通知。

```cpp theme={null}
bool CustomGameXErrorCallback(_In_ HRESULT hr, _In_ const char* msg, _In_ void* context)  
{  
   // Capture the error to the game's custom logger  
   CustomGameLogger* logger = (CustomGameLogger*)context;  
   logger->ReportGameRuntimeIssue(hr, msg);  
  
   // Don't ignore the error. For example, continue on and do whatever  
   // is specified by XErrorSetOptions().  
   return true;  
}  
  
void SomeGameFunction()  
{  
   // Previously initialize the game's custom logger  
   // CustomGameLogger* m_logger = InitializeLogger();  
   // ...  
  
   // Setup how you want to handle XErrors  
   XErrorSetOptions(DebugBreakOnError, FailFastOnError);
   XErrorSetCallback(CustomGameXErrorCallback, m_logger);
}  
```

## Requirements

**头文件：** XError.h

**库：** xgameruntime.lib

**受支持的平台：** Windows、XBOX One 系列主机和 XBOX Series 主机

## See also

[XError members](/reference/system/xerror/xerror_members)\
[XErrorSetCallback](/reference/system/xerror/functions/xerrorsetcallback)
