Learn Javascript Error Object tutorials with examples

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 , message have a supports in all browsers. All other properties like fileName and lineNumber will not have support all browsers Syntax

This is the general syntax.

new InternalError(Messages,FileName,LineNumber)  

and the parameters are

  • Messages indicate detailed descriptive messages
  • Filename indicates on which error occurred line number indicates on which
  • line number In tells the line number occurred

All the above three parameters are optional.

How to Declare Error Object

Let’s see an steps how to declare an error object

Error object can be created in following ways

  • using the new operator.
  • without a new operator.

Both return the same Error object and behavior also same.

So Object can be created using with 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?

When ever an error throwns, 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 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;  
 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 their explains

Error Name



This error thrown during Eval() function execution


This error thrown during runtime error occurred in the javascript engine


This error occurred during accessing number variables range with the outside of a defined range


This error returns during execution of references manipulation


This error occurred during execution of code in Eval(code)method


returned this error for instance variables are not valid types


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 given object type

try {  
    var instanceVariable;  
} 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 record. When we are declaring a custom error, we need to set standard properties like name and messages. As of now, we have seen the different errors as listed above. You can also create your own Errors by extending the Error object.

class DuplicateError extends Error {  
  constructor(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 custom error by extending Error object 
  • In the constructor, the first line should call to Super class 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

Please share it or click on the google +1 button.

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.