From 2a942748f2256968097bcdec5d41f8a21295c3a5 Mon Sep 17 00:00:00 2001 From: puru-khedre <101047627+puru-khedre@users.noreply.github.com> Date: Wed, 5 Oct 2022 05:40:38 +0530 Subject: [PATCH] Update 102-utilizing-error-objects.md (#2136) --- .../102-utilizing-error-objects.md | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) 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