JavaScript & jQuery Tutorials

JS Home Right Arrow JS Reference Right Arrow ReferenceError

ReferenceError constructor

In JavaScript you create a ReferenceError instance using the constructor ReferenceError.

Description

Creates a ReferenceError object when trying to dereference a non-existant variable.

Syntax


Signature Description
anError = new ReferenceError([message]);In JavaScript you create a range error instance using the constructor ReferenceError.

Parameters


Parameter Description
messageAn optional description of the error.

Class Properties


Class Property Description
prototypeEnables property assignment to objects of type ReferenceError.

Instance Properties


Instance Property Description
constructorReturns a reference to the ReferenceError function that created the prototype.
nameAn error name.

Class Methods

None.

Instance Methods

None.

Examples


Let's look at some examples of throwing a reference error within our code.




// Throw an error.
try {
  throw new ReferenceError();
} catch (e) {
    alert('A reference error: ' + e.name);
}  

// Throw an error with a message.
try {
  throw new ReferenceError('Throwing a reference error.');
} catch (e) {
    alert('A reference error with message: ' + e.name + ': ' + e.message);
}  


Press the button below to action the above code:



We can also create our own ReferenceError instances as the example below shows.




// Create our own ReferenceError object.
function ourReferenceError(message) {  
  this.name = 'OurReferenceError';  
  this.message = message || 'Default Message Used When No Message Passed';
}   

// Inherit from the ReferenceError prototype.
ourReferenceError.prototype = new ReferenceError();  
ourReferenceError.prototype.constructor = ourReferenceError;   

// Throw an error.
try {  
  throw new ourReferenceError();  
} catch (e) {  
  alert('Our Error: ' + e.name + ' Message: ' + e.message);
}   

// Throw an error and a message.
try {  
  throw new ourReferenceError('A Message we have passed');  
} catch (e) {  
  alert('Our Error: ' + e.name + ' Our Message: ' + e.message);
} finally {  
  alert('A finally statement will always be executed!');
}   


Press the button below to action the above code:



Related Tutorials

JavaScript Advanced Tutorials - Lesson 2 - Errors




  


go to home page Homepage go to top of page Top