/    /  Javascript-Error Handling

Javascript-Error Handling

 

An error in JavaScript is an object and which is later thrown to halt the program.

you want to create a new error in JavaScript we call the appropriate constructor function.

 

Example:

 

Create a new, generic error.

 

const err = new Error("Something bad happened!");
creating an error object it is also possible to omit the new keyword:
const err = Error("Something bad happened!");

 

The error object presents three properties:

 

  • message: a string with the error message.
  • name: the error’s type.
  • stack: a stack trace of functions execution.

 

Many types of errors in JavaScript:

 

  • Error
  • EvalError
  • InternalError
  • RangeError
  • ReferenceError
  • SyntaxError
  • TypeError
  • URIError

 

All these error types are actual constructor functions(new error object).

 

 An example of TypeError :

 

const name = "Jules";
name = "Caty";

 

// TypeError: Assignment to constant variable.

 

example of SyntaxError :

 

misspell language keywords

 

va x = '33';

 

// SyntaxError: Unexpected identifier

 

OR

 

Use reserved keywords in wrong places: await outside of an async function.

 

function wrong(){
    await 99;
}

wrong();

 

// SyntaxError: await is only valid in async function

 

built-in errors:

  • DOMException
  • DOMError: deprecated and no longer used today.

 

Family of errors related to Web APIs.

 

document.body.appendChild(document.cloneNode(true));

 

The result:

 

Uncaught DOMException: Node.appendChild: May not add a Document as a child