> ## 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.

# XErrorSetCallback

> XErrorSetCallback

# XErrorSetCallback

指定游戏 OS 或游戏运行时出现错误时调用的回调。

## Syntax

```cpp theme={null}
void XErrorSetCallback(  
         XErrorCallback* callback,  
         void* context  
)  
```

### Parameters

*callback*   \_In\_\
类型：XErrorCallback\*

指向要调用的游戏定义回调的指针。传入 `nullptr` 清除该回调。

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

回调的上下文指针。

### Return value

类型：void

## Remarks

<Note>此函数在时间敏感线程上调用不安全。有关详细信息，请参阅[时间敏感线程](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)。</Note>

使用此函数在游戏中指定发生错误时系统应调用的 [XErrorCallback](/reference/system/xerror/functions/xerrorcallback) 回调，并使用 [XErrorSetOptions](/reference/system/xerror/functions/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 主机

## Conceptual documentation

* [时间敏感线程](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## See also

[XError members](/reference/system/xerror/xerror_members)\
[XErrorCallback](/reference/system/xerror/functions/xerrorcallback)


## Related topics

- [XErrorCallback](/zh-CN/reference/system/xerror/functions/xerrorcallback.md)
- [XError members](/zh-CN/reference/system/xerror/xerror_members.md)
- [PartyManager::SetMemoryCallbacks](/zh-CN/services/playfab/multiplayer/networking/reference/classes/PartyManager/methods/partymanager_setmemorycallbacks.md)
- [PartyXblManager::SetMemoryCallbacks](/zh-CN/services/playfab/multiplayer/networking/xblreference/classes/PartyXblManager/methods/partyxblmanager_setmemorycallbacks.md)
- [XGameUiShowErrorDialogUiCallback](/zh-CN/reference/system/xgameui/functions/xgameuishowerrordialoguicallback.md)
