1# ErrorObserver 2 3The 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#errormanageronerror) to listen for errors that occur in the current application. 4 5> **NOTE** 6> 7> 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. 8 9## Modules to Import 10 11```ts 12import { errorManager } from '@kit.AbilityKit'; 13``` 14 15## ErrorObserver.onUnhandledException 16 17onUnhandledException(errMsg: string): void; 18 19Called when an unhandled exception occurs in the JS runtime. 20 21**Atomic service API**: This API can be used in atomic services since API version 11. 22 23**System capability**: SystemCapability.Ability.AbilityRuntime.Core 24 25**Parameters** 26 27| Name| Type| Mandatory| Description| 28| -------- | -------- | -------- | -------- | 29| errMsg | string | Yes| Message and error stack trace about the exception.| 30 31**Example** 32 33```ts 34import { errorManager } from '@kit.AbilityKit'; 35import { BusinessError } from '@kit.BasicServicesKit'; 36 37let observer: errorManager.ErrorObserver = { 38 onUnhandledException(errorMsg) { 39 console.error('onUnhandledException, errorMsg: ', errorMsg); 40 } 41}; 42 43try { 44 errorManager.on('error', observer); 45} catch (error) { 46 console.error(`registerErrorObserver failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 47} 48``` 49 50## ErrorObserver.onException<sup>10+</sup> 51 52onException?(errObject: Error): void; 53 54Called when an exception occurs during the application running. 55 56**Atomic service API**: This API can be used in atomic services since API version 11. 57 58**System capability**: SystemCapability.Ability.AbilityRuntime.Core 59 60**Parameters** 61 62| Name| Type| Mandatory| Description| 63| -------- | -------- | -------- | -------- | 64| errObject | Error | Yes| Event name, message, and error stack of the exception.| 65 66**Example** 67 68```ts 69import { errorManager } from '@kit.AbilityKit'; 70import { BusinessError } from '@kit.BasicServicesKit'; 71 72let observer: errorManager.ErrorObserver = { 73 onUnhandledException(errorMsg) { 74 console.error('onUnhandledException, errorMsg: ', errorMsg); 75 }, 76 onException(errorObj) { 77 console.log('onException, name: ', errorObj.name); 78 console.log('onException, message: ', errorObj.message); 79 if (typeof (errorObj.stack) === 'string') { 80 console.log('onException, stack: ', errorObj.stack); 81 } 82 } 83}; 84 85try { 86 errorManager.on('error', observer); 87} catch (error) { 88 console.error(`registerErrorObserver failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 89} 90``` 91