Error cause
The cause data property of an Error instance indicates the specific original cause of the error.
It is used when catching and re-throwing an error with a more-specific or useful error message in order to still have access to the original error.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 93 | 93 | 91 | 15 | 93 | 15 | |
| Built-in object | ||||||
Error.Error.options cause parameter `options.cause` parameter | 93 | 93 | 91 | 15 | 93 | 15 |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Notes 1 item(s)
Implementation note
- Before version 125, default console logging for `Error` objects does not print the cause.
Notes 1 item(s)
Implementation note
- Before version 125, default console logging for `Error` objects does not print the cause.
Notes 1 item(s)
Implementation note
- Default console logging for `Error` objects does not print the cause.
Notes 1 item(s)
Implementation note
- Before version 125, default console logging for `Error` objects does not print the cause.
Notes 1 item(s)
Implementation note
- Default console logging for `Error` objects does not print the cause.
Syntax
JAVASCRIPT
try {
const data = JSON.parse(invalidJson);
} catch (err) {
throw new Error('Failed to load data', { cause: err });
}
// Access the original error via err.cause Live demo
Use cases
-
Using Error cause
The cause data property of an Error instance indicates the specific original cause of the error.
Cautions
- No specific concerns. Stable across all major browsers.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.