ReferenceError
constructor
JS Home <<
JS Reference <<
ReferenceError
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 |
---|---|
message | An optional description of the error. |
Class Properties
Class Property | Description |
---|---|
prototype | Enables property assignment to objects of type ReferenceError . |
Instance Properties
Instance Property | Description |
---|---|
constructor | Returns a reference to the ReferenceError function that created the prototype. |
name | An 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);
}
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!');
}
Related Tutorials
JavaScript Advanced Tutorials - Lesson 2 - Errors