1# ExceptionPrompt 2 3 4The exception prompt component is used to show an error message when an error arises. 5 6 7> **NOTE** 8> 9> This component is supported since API version 11. Updates will be marked with a superscript to indicate their earliest API version. 10 11 12## Modules to Import 13 14```ts 15import { ExceptionPrompt, PromptOptions, MarginType } from '@kit.ArkUI' 16``` 17 18 19## Child Components 20 21Not supported 22 23## Attributes 24 25The [universal attributes](ts-component-general-attributes.md) are not supported. 26 27## ExceptionPrompt 28 29ExceptionPrompt({ options: PromptOptions, onTipClick?: ()=>void, onActionTextClick?: ()=>void }) 30 31**Decorator**: @Component 32 33**Atomic service API**: This API can be used in atomic services since API version 12. 34 35**System capability**: SystemCapability.ArkUI.ArkUI.Full 36 37**Parameters** 38 39 40| Name| Type| Mandatory| Decorator| Description| 41| -------- | -------- | -------- | -------- | -------- | 42| options | [PromptOptions](#promptoptions) | Yes| \@Prop | Exception prompt configuration.| 43| onTipClick | ()=>void | No| - | Callback invoked when the prompt text on the left is clicked.| 44| onActionTextClick | ()=>void | No| - | Callback invoked when the icon on the right is clicked.| 45 46## PromptOptions 47 48Defines the exception prompt options. 49 50**Atomic service API**: This API can be used in atomic services since API version 12. 51 52**System capability**: SystemCapability.ArkUI.ArkUI.Full 53 54| Name| Type| Mandatory| Description| 55| -------- | -------- | -------- | -------- | 56| icon | [ResourceStr](ts-types.md#resourcestr) | No| Icon style of the exception prompt.| 57| symbolStyle<sup>18+</sup> | [SymbolGlyphModifier](ts-universal-attributes-attribute-modifier.md) | No| Symbol icon style of the exception prompt, which has higher priority than **icon**.<br>**Atomic service API**: This API can be used in atomic services since API version 18.| 58| tip | [ResourceStr](ts-types.md#resourcestr) | No| Text content of the exception prompt.<br>By default, the following text resources are provided:<br>1. **ohos_network_not_connected**: displayed when no Internet connection.<br>2. **ohos_network_connected_unstable**: displayed when the Internet connection is unstable.<br>3. **ohos_unstable_connect_server**: displayed when the server fails to be connected.<br>4. **ohos_custom_network_tips_left**: displayed when an Internet connection is available but the location fails to be obtained.| 59| marginType | [MarginType](#margintype) | Yes| Margin type of the exception prompt.| 60| actionText | [ResourceStr](ts-types.md#resourcestr) | No| Text of the icon on the right of the exception prompt.| 61| marginTop | [Dimension](ts-types.md#dimension10) | Yes| Top margin of the exception prompt.| 62| isShown | boolean | No| Whether the exception prompt is displayed.<br>**true**: The exception prompt is displayed.<br>**false**: The exception prompt is displayed.| 63 64## MarginType 65 66Defines the margin type. 67 68**Atomic service API**: This API can be used in atomic services since API version 12. 69 70**System capability**: SystemCapability.ArkUI.ArkUI.Full 71 72| Name| Value| Description| 73| -------- | -------- | -------- | 74| DEFAULT_MARGIN | 0 | Default margin:<br>Margin 1: referenced from **ohos_id_card_margin_start**.<br>Margin 2: referenced from **ohos_id_card_margin_end**.| 75| FIT_MARGIN | 1 | Adaptable margin:<br> Margin 1: referenced from **ohos_id_max_padding_start**.<br> Margin 2: referenced from **ohos_id_max_padding_end**.| 76 77## Events 78The [universal events](ts-component-general-events.md) are not supported. 79 80## Example 81### Example 1: Configuring an Exception Prompt 82 83This example demonstrates how to configure an exception prompt, including the exception icon, text, margin, and the text content of the right-side icon button. 84 85```ts 86import { ExceptionPrompt, PromptOptions, MarginType } from '@kit.ArkUI' 87 88@Entry 89@Component 90struct Index { 91 @State options: PromptOptions = { 92 icon: $r('sys.media.ohos_ic_public_fail'), 93 tip: 'Error', 94 marginType: MarginType.DEFAULT_MARGIN, 95 actionText: 'Set network', 96 marginTop: 80, 97 isShown: true, 98 } 99 100 build() { 101 Column() { 102 ExceptionPrompt({ 103 options: this.options, 104 onTipClick: () => { 105 // Handle clicks on the left text to switch to a connected state. 106 }, 107 onActionTextClick: () => { 108 // Handle clicks on the Set network button to open the network settings dialog box. 109 }, 110 }) 111 } 112 } 113} 114``` 115 116 117 118### Example 2: Setting a Dialog-Type Exception Prompt 119 120This example uses a custom dialog box to set a dialog-type exception prompt. 121 122```ts 123import { ExceptionPrompt, PromptOptions, MarginType } from '@kit.ArkUI' 124 125@CustomDialog 126struct CustomDialogExample { 127 @Link textValue: string; 128 @Link inputValue: string; 129 @State options: PromptOptions = { 130 icon: $r('sys.media.ohos_ic_public_fail'), 131 tip: 'Error', 132 marginType: MarginType.DEFAULT_MARGIN, 133 actionText: 'Settings', 134 marginTop: 5, 135 isShown: true, 136 }; 137 cancel: () => void = () => { 138 }; 139 confirm: () => void = () => { 140 }; 141 controller?: CustomDialogController; 142 143 // To pass multiple other controllers into a CustomDialog to open another or several other custom dialog boxes within it, 144 // place the controller pointing to itself last. 145 build() { 146 Column() { 147 ExceptionPrompt({ 148 options: this.options, 149 }) 150 TextInput({ placeholder: '', text: this.textValue }).margin({ top: 70 }).height(60).width('90%') 151 .onChange((value: string) => { 152 this.textValue = value; 153 }) 154 Text('Are you sure you want to change the text?').fontSize(16).margin({ bottom: 10 }) 155 Flex({ justifyContent: FlexAlign.SpaceAround }) { 156 Button('No') 157 .onClick(() => { 158 this.controller?.close(); 159 this.cancel(); 160 }).backgroundColor(0xffffff).fontColor(Color.Black) 161 Button('OK') 162 .onClick(() => { 163 this.inputValue = this.textValue; 164 this.controller?.close(); 165 this.confirm(); 166 }).backgroundColor(0xffffff).fontColor(Color.Red) 167 }.margin({ bottom: 10 }) 168 } 169 } 170} 171 172@Entry 173@Component 174struct Index1 { 175 @State ButtonText: string = ''; 176 @State MAP_HEIGHT: string = '30%'; 177 @State duration: number = 2500; 178 @State tips: string = ''; 179 @State actionText: string = ''; 180 controller: TextInputController = new TextInputController(); 181 cancel: () => void = () => { 182 }; 183 confirm: () => void = () => { 184 }; 185 @State options: PromptOptions = { 186 icon: $r('sys.media.ohos_ic_public_fail'), 187 tip: '', 188 marginType: MarginType.DEFAULT_MARGIN, 189 actionText: '', 190 marginTop: 80, 191 isShown: true, 192 } 193 @State textValue: string = ''; 194 @State inputValue: string = 'click me'; 195 dialogController: CustomDialogController | undefined = new CustomDialogController({ 196 builder: CustomDialogExample({ 197 cancel: this.onCancel, 198 confirm: this.onAccept, 199 textValue: $textValue, 200 inputValue: $inputValue, 201 }), 202 cancel: this.existApp, 203 autoCancel: true, 204 alignment: DialogAlignment.Bottom, 205 offset: { dx: 0, dy: -20 }, 206 gridCount: 4, 207 customStyle: false, 208 }) 209 210 aboutToDisappear() { 211 this.dialogController = undefined; // Set dialogController to undefined. 212 } 213 214 onCancel() { 215 console.info('Callback when the first button is clicked'); 216 } 217 218 onAccept() { 219 console.info('Callback when the second button is clicked'); 220 } 221 222 existApp() { 223 console.info('Click the callback in the blank area'); 224 } 225 226 build() { 227 Column() { 228 Button('Click Me') 229 .width('30%') 230 .margin({ top: 420 }) 231 .zIndex(999) 232 .onClick(() => { 233 if (this.dialogController != undefined) { 234 this.dialogController.open(); 235 } 236 }) 237 } 238 .height('100%') 239 .width('100%') 240 } 241} 242``` 243 244 245 246### Example 3: Setting the Symbol Icon 247 248This example demonstrates how to use **symbolStyle** in **PromptOptions** to set custom symbol icons. 249 250```ts 251import { ExceptionPrompt, MarginType, SymbolGlyphModifier } from '@kit.ArkUI' 252 253@Entry 254@Component 255struct Index { 256 build() { 257 Column() { 258 ExceptionPrompt({ 259 options: { 260 icon: $r('sys.symbol.house'), 261 tip: 'Exception prompt Exception prompt Exception prompt', 262 marginType: MarginType.DEFAULT_MARGIN, 263 actionText: 'Set network Set network Set network Set network', 264 marginTop: 80, 265 isShown: true, 266 }, 267 }) 268 ExceptionPrompt({ 269 options: { 270 icon: $r('sys.symbol.house'), 271 symbolStyle: new SymbolGlyphModifier($r('sys.symbol.bell')).fontColor([Color.Red]), 272 tip: 'Exception prompt Exception prompt Exception prompt', 273 marginType: MarginType.DEFAULT_MARGIN, 274 actionText: 'Set network Set network Set network Set network', 275 marginTop: 80, 276 isShown: true, 277 }, 278 }) 279 } 280 } 281} 282``` 283 284 285