Site icon i2tutorials

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:

 

 

Many types of errors in JavaScript:

 

 

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:

 

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

 

Exit mobile version