Update 102-utilizing-error-objects.md (#2136)
parent
ebdbbcc616
commit
2a942748f2
1 changed files with 34 additions and 1 deletions
@ -1 +1,34 @@ |
||||
# Utilizing error objects |
||||
# Utilizing error objects |
||||
|
||||
When a runtime error occurs, a new `Error` object is created and thrown. With this `Error` object, we can determine the type of the Error and handle it according to its type. |
||||
|
||||
## Types of Errors: |
||||
|
||||
Besides error constructors, Javascript also has other core Error constructors. |
||||
|
||||
- [`AggregateError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) |
||||
- [`EvalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError) |
||||
- [`InternalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/InternalError) |
||||
- [`RangeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError) |
||||
- [`ReferenceError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError) |
||||
- [`SyntaxError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError) |
||||
|
||||
## Example |
||||
|
||||
```js |
||||
try { |
||||
willGiveErrorSometime(); |
||||
} catch (error) { |
||||
if (error instanceof RangeError) { |
||||
rangeErrorHandler(error); |
||||
} else if (error instanceof ReferenceError) { |
||||
referenceErrorHandle(error); |
||||
} else { |
||||
errorHandler(error); |
||||
} |
||||
} |
||||
``` |
||||
|
||||
<ResourceGroupTitle>Free Content</ResourceGroupTitle> |
||||
<BadgeLink colorScheme='yellow' badgeText='Read' href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error'>Error Object - MDN</BadgeLink> |
||||
<BadgeLink colorScheme='yellow' badgeText='Read' href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling'>Control flow & Error handling - MDN</BadgeLink> |
||||
|
Loading…
Reference in new issue