AppDividend
Latest Code Tutorials

Javascript Error Object Tutorial With Example

0

Javascript Error Object Tutorial With Example is today’s topic. The Error constructor creates an error object. Instances of Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. Runtime errors result in new Error objects being created and thrown. The Error object provides error information when an error occurs.

Javascript Error Object Tutorial With Example

The syntax of Error in Javascript is following.

new Error([message[, fileName[, lineNumber]]])

message:

Optional. A human-readable description of the error.

fileName:

Optional. The value for the file property on the created Error object. Defaults to the name of the file containing the code that called the Error() constructor.

lineNumber:

Optional. The value for the lineNumber property on the created an Error object. Defaults to the line number containing the Error() constructor invocation.

Let’s see the Error in an example.

// app.js

const x = new Error('Oops!! There is a mistake');
console.log(x);

The output is following.

 

Javascript Error Tutorial With Example

The global Error object contains no methods of its own. However, it does inherit some methods from the prototype chain.

Javascript Error types

There are following types of Error in Javascript.

EvalError: Creates an instance representing an error that occurs regarding the global function eval().

InternalError:  Creates an instance representing an error that occurs when an internal error in the JavaScript engine is thrown. E.g. “too much recursion.”

RangeError: Creates an instance representing an error that occurs when a numeric variable or parameter is outside of its valid range.

ReferenceError: Creates an instance representing an error that occurs when de-referencing an invalid reference.

SyntaxError: Creates an instance representing a syntax error that occurs while parsing code in eval().

TypeError: Creates an instance representing an error that occurs when a variable or parameter is not of a valid type.

URIError: Creates an instance representing an error that occurs when encodeURI() or decodeURI() are passed invalid parameters.

Finally, Javascript Error Object Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.