> ## 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 或遊戲執行階段發生錯誤時所呼叫的回呼。

## 語法

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

### 參數

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

要呼叫之遊戲定義回呼的指標。傳入 `nullptr` 會清除回呼。

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

回呼的內容指標。

### 傳回值

類型：void

## 備註

<Note>在時間敏感的執行緒上呼叫此函式並不安全。如需詳細資訊，請參閱[時間敏感的執行緒](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)。</Note>

使用此函式來指定遊戲中發生錯誤時系統應呼叫的 [XErrorCallback](/zh-TW/reference/system/xerror/functions/xerrorcallback) 回呼，並使用 [XErrorSetOptions](/zh-TW/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);
}  
```

## 需求

**標頭：** XError.h

**程式庫：** xgameruntime.lib

**支援的平台：** Windows、XBOX One 系列主機和 XBOX Series 主機

## 概念文件

* [時間敏感的執行緒](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## 另請參閱

[XError 成員](/zh-TW/reference/system/xerror/xerror_members)\
[XErrorCallback](/zh-TW/reference/system/xerror/functions/xerrorcallback)


## Related topics

- [XErrorSetCallback](/es/reference/system/xerror/functions/xerrorsetcallback.md)
- [XErrorCallback](/reference/system/xerror/functions/xerrorcallback.md)
