Files
developer-roadmap/src/data/roadmaps/javascript/content/error-objects@-z-4VTaC3tOThqChgyoMs.md
T
Vedansh 5c2cc7a6da Improved JavaScript Roadmap (#6779)
* - Fixed Spacing and EOL.
- Fixed Wrong Spellings.
- Improved Examples.

* ## Adding Content for :

- Add `apply()`
- Add `bind()`
- Add `call()`
2024-08-30 09:40:33 +01:00

2.0 KiB

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. Like

  • AggregateError - A collection of errors thrown simultaneously.
  • EvalError - An error occurred during the evaluation of a JavaScript expression.
  • InternalError - An internal JavaScript error, often indicating a bug in the engine.
  • RangeError - A value is outside the allowed range for a given operation.
  • ReferenceError - A variable or object is referenced before it's declared or doesn't exist.
  • SyntaxError - The code contains incorrect syntax, preventing it from being parsed.

Example

try {
  willGiveErrorSometime();
} catch (error) {
  if (error instanceof RangeError) {
    rangeErrorHandler(error);
  } else if (error instanceof ReferenceError) {
    referenceErrorHandle(error);
  } else {
    errorHandler(error);
  }
}

Visit the following resources to learn more: