diff --git a/content/roadmaps/106-javascript/content/107-javascript-control-flow/100-exception-handling/102-utilizing-error-objects.md b/content/roadmaps/106-javascript/content/107-javascript-control-flow/100-exception-handling/102-utilizing-error-objects.md index 02e1356cd..9f9572d7c 100644 --- a/content/roadmaps/106-javascript/content/107-javascript-control-flow/100-exception-handling/102-utilizing-error-objects.md +++ b/content/roadmaps/106-javascript/content/107-javascript-control-flow/100-exception-handling/102-utilizing-error-objects.md @@ -1 +1,34 @@ -# Utilizing error objects \ No newline at end of file +# 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); + } +} +``` + +Free Content +Error Object - MDN +Control flow & Error handling - MDN