js-apis-inner-application-errorObserver.md 1.3 KB
Newer Older
1 2
# ErrorObserver

G
Gloria 已提交
3
The **ErrorObserver** module defines an observer to listen for application errors. It can be used as an input parameter in [errorManager.on](js-apis-app-ability-errorManager.md#errormanageron) to listen for errors that occur in the current application.
4

G
Gloria 已提交
5 6 7 8 9 10 11 12 13 14 15
> **NOTE**
> 
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

## Modules to Import

```ts
import errorManager from '@ohos.app.ability.errorManager';
```

## ErrorObserver.onUnhandledException
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

onUnhandledException(errMsg: string): void;

Called when an unhandled exception occurs in the JS runtime.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| errMsg | string | No| Message and error stack trace about the exception.|

**Example**

```ts
G
Gloria 已提交
32
import errorManager from '@ohos.app.ability.errorManager';
33 34

let observer = {
G
Gloria 已提交
35 36 37
  onUnhandledException(errorMsg) {
    console.error('onUnhandledException, errorMsg: ', errorMsg);
  }
38
};
G
Gloria 已提交
39 40 41 42 43 44 45

try {
    errorManager.on('error', observer);
} catch (error) {
    console.error('registerErrorObserver failed, error.code: ${error.code}, error.message: ${error.message}');
}
```
46
```