ErrorObserver
ErrorObserver
定义异常监听,可以作为ErrorManager.on的入参监听当前应用发生的异常。
ErrorObserver.onUnhandledException
onUnhandledException(errMsg: string): void;
将在js运行时引发用户未捕获的异常时调用。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
errMsg | string | 否 | 有关异常的消息和错误堆栈跟踪。 |
示例:
import errorManager from '@ohos.app.ability.errorManager';
let observer = {
onUnhandledException(errorMsg) {
console.error('onUnhandledException, errorMsg: ', errorMsg);
}
};
try {
errorManager.on('error', observer);
} catch (error) {
console.error('registerErrorObserver failed, error.code: ${error.code}, error.message: ${error.message}');
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
ErrorObserver.onException
onException?(errObject: Error): void;
将在应用运行异常时调用。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
errObject | Error | 是 | 有关异常事件名字、消息和错误堆栈信息的对象。 |
示例:
import errorManager from '@ohos.app.ability.errorManager';
let observer = {
onUnhandledException(errorMsg) {
console.error('onUnhandledException, errorMsg: ', errorMsg);
},
onException(errorObj) {
console.log('onException, name: ', errorObj.name);
console.log('onException, message: ', errorObj.message);
if (typeof(errorObj.stack) === 'string') {
console.log('onException, stack: ', errorObj.stack);
}
}
};
try {
errorManager.on('error', observer);
} catch (error) {
console.error('registerErrorObserver failed, error.code: ${error.code}, error.message: ${error.message}');
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20