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 and message properties supports in all browsers. All other properties like fileName and lineNumber will not have support all browsers Syntax This is the general syntax. All the browsers don’t have support for all these parameters.

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 error occurred both three parameters are optional.

 Error Object Declaration and throwing examples 

Normally instance of Error object can be created using the new operator. another way is creating 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`
```We will see the example where the error is thrown to the client using try/catch blocks of code. try/catch blocks help to handle the exceptions/errors in javascript applications.  

```markup
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 Initialised 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;  
    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 their explains

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 number variables range with the outside of a defined range

ReferenceError

This error returns during execution of references manipulation

SyntaxError

This error occurred during execution of code in 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 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 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) {  
    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 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.

Similar Posts