Learn Javascript Error Object tutorials with examples
- Admin
- Dec 31, 2023
- Javascript
The error
is javascript standard object like Boolean
, Function
, and Object
objects. This will be thrown when a runtime error occurs.
It has name
, message
, fileName
and lineNumber
properties name
, and message
have support in all browsers. All other properties like fileName
and lineNumber
will not have to support all browsers Syntax:
General syntax.
new InternalError(Messages, FileName, LineNumber);
and the parameters are
Messages
indicate detailed descriptive messagesFilename
indicates which error occurred line number indicates on whichline number
In tells the line number that occurred
All the above three parameters are optional.
How to Declare Error Object
Let’s see the steps on how to declare an error object.
Error object creates in the following ways
- using the
new
operator. - without a new operator.
Both return the same Error object and the behavior is also the same.
So objects can be created using or without a new operator.
var error = new Error("Error with new keyword");
var error1 = Error("Error without new keyword");
console.log(typeof error); // returns object
console.log(typeof error1); // returns object`
How to handle exceptions in Javascript?
Whenever an error is thrown, the Application stops its execution. We have to handle the errors.
javascript try and catch keywords when an error is thrown to the client
try
/catch
blocks help to handle the exceptions/errors in javascript applications.
try {
throw new Error("Throwing Error object");
} catch (e) {
console.log(typeof e); // returns Object
console.log(e.name); // returns Error
console.log(e.message); // returns Throwing Error object
}
From the below example,
- Declared and initialized instance variable with null, got an exception when tried to access unknown method. The error is thrown and caught in the catch block.
Error.name returned ‘TypeError’. This is one of the error types.
Error.message returns a detailed error message like ’ Cannot read property ‘method’ of null’ and this message is different for each browser type.
and stack trace is VM433:2 Uncaught TypeError: Cannot read property ‘method’ of null at 2:26
try {
var nullInstance = null;
nullInstance.method();
} catch (e) {
console.log(typeof e);
// above returns VM433:2 Uncaught TypeError: Cannot read property 'method' of null
// at :2:26
console.log(e.name); // returns TypeError
console.log(e.message); // returns Cannot read property 'method' of null
}
There are several error types available in javascript. Please find the below list of error types and explanations.
Error Name
Description
EvalError
This error thrown during Eval() function execution
InteralError
This error thrown during runtime error occurred in the javascript engine
RangeError
This error occurred during accessing the number variables range with the outside of a defined range
ReferenceError
This error returns during the execution of references manipulation
SyntaxError
This error occurred during the execution of code in the Eval(code)method
TypeError
returned this error for instance variables are not valid types
URIError
This error occurred for invalid parameters passed for encodeURI/decodeURI
Error and all the above Error types extend from Error.prototype which extends from global object Object.prototype.
Let us go through some examples using Error Object.
Error Object instanceOf example
This instanceOf() method is to check specific error types. This checks instance is of the given object type.
try {
var instanceVariable;
instanceVariable.method();
} catch (e) {
console.log("error ", e);
if (e instanceof EvalError) {
console.log(e.name + ": " + e.message);
} else if (e instanceof TypeError) {
console.log(e.name + ": " + e.message); // returns TypeError: Cannot read property 'bar' of undefined
}
}
Custom Error Declaration extending Error Object
In our Application development, we have to write our Application Specific errors like DuplicateError occurs when saving duplicate error records. When we are declaring a custom error, we need to set standard properties like names and messages. As of now, we have seen the different errors listed above. You can also create your Errors by extending the Error object.
class DuplicateError extends Error {
constructor(message) {
super(message);
this.name = "DuplicateError";
}
}
try {
throw new DuplicateError("Duplicate Record found");
} catch (err) {
console.log(err.message); // Duplicate Record found
console.log(err.name); // DuplicateError
console.log(err.stack); // prints complete stacktrace of a error
}
Steps to create Application-Specific error
- Create a custom error by extending the Error object
- In the constructor, the first line should call to Superclass constructor to set the message property.
- In your code, using throw new code throw custom exception which we can declare this code in try/catch block to handle custom exception handling