1e41f4b71Sopenharmony_ci# ErrorObserver
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci定义异常监听,可以作为[ErrorManager.on](js-apis-app-ability-errorManager.md#errormanageronerror)的入参监听当前应用发生的异常。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci> 
7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## 导入模块
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```ts
12e41f4b71Sopenharmony_ciimport { errorManager } from '@kit.AbilityKit';
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## ErrorObserver.onUnhandledException
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_cionUnhandledException(errMsg: string): void;
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci将在js运行时引发用户未捕获的异常时调用。
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**参数:**
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
28e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
29e41f4b71Sopenharmony_ci| errMsg | string | 是 | 有关异常的消息和错误堆栈跟踪。 |
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci**示例:**
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci```ts
34e41f4b71Sopenharmony_ciimport { errorManager } from '@kit.AbilityKit';
35e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_cilet observer: errorManager.ErrorObserver = {
38e41f4b71Sopenharmony_ci  onUnhandledException(errorMsg) {
39e41f4b71Sopenharmony_ci    console.error('onUnhandledException, errorMsg: ', errorMsg);
40e41f4b71Sopenharmony_ci  }
41e41f4b71Sopenharmony_ci};
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_citry {
44e41f4b71Sopenharmony_ci  errorManager.on('error', observer);
45e41f4b71Sopenharmony_ci} catch (error) {
46e41f4b71Sopenharmony_ci  console.error(`registerErrorObserver failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
47e41f4b71Sopenharmony_ci}
48e41f4b71Sopenharmony_ci```
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci## ErrorObserver.onException<sup>10+</sup>
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_cionException?(errObject: Error): void;
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci将在js运行时引发用户未捕获的异常时调用。
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci**参数:**
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
63e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
64e41f4b71Sopenharmony_ci| errObject | Error | 是 | 有关异常事件名字、消息和错误堆栈信息的对象。 |
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci**示例:**
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci```ts
69e41f4b71Sopenharmony_ciimport { errorManager } from '@kit.AbilityKit';
70e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_cilet observer: errorManager.ErrorObserver = {
73e41f4b71Sopenharmony_ci  onUnhandledException(errorMsg) {
74e41f4b71Sopenharmony_ci    console.error('onUnhandledException, errorMsg: ', errorMsg);
75e41f4b71Sopenharmony_ci  },
76e41f4b71Sopenharmony_ci  onException(errorObj) {
77e41f4b71Sopenharmony_ci    console.log('onException, name: ', errorObj.name);
78e41f4b71Sopenharmony_ci    console.log('onException, message: ', errorObj.message);
79e41f4b71Sopenharmony_ci    if (typeof (errorObj.stack) === 'string') {
80e41f4b71Sopenharmony_ci      console.log('onException, stack: ', errorObj.stack);
81e41f4b71Sopenharmony_ci    }
82e41f4b71Sopenharmony_ci  }
83e41f4b71Sopenharmony_ci};
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_citry {
86e41f4b71Sopenharmony_ci  errorManager.on('error', observer);
87e41f4b71Sopenharmony_ci} catch (error) {
88e41f4b71Sopenharmony_ci  console.error(`registerErrorObserver failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
89e41f4b71Sopenharmony_ci}
90e41f4b71Sopenharmony_ci```
91