RangeError
constructor
JS Home <<
JS Reference <<
RangeError
In JavaScript you create a range error instance using the constructor RangeError
.
Description
Creates a RangeError
object when a numeric variable or parameter is outside of its valid range.
Syntax
Signature | Description |
---|---|
anError = new RangeError([message]); | In JavaScript you create a range error instance using the constructor RangeError . |
Parameters
Parameter | Description |
---|---|
message | An optional description of the error. |
Class Properties
Class Property | Description |
---|---|
prototype | Enables property assignment to objects of type RangeError . |
Instance Properties
Instance Property | Description |
---|---|
constructor | Returns a reference to the RangeError 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 range error within our code.
// Throw an error.
try {
throw new RangeError();
} catch (e) {
alert('A range error: ' + e.name);
}
// Throw an error with a message.
try {
throw new RangeError('Throwing a range error.');
} catch (e) {
alert('A range error with message: ' + e.name + ': ' + e.message);
}
We can also create our own RangeError
instances as the example below shows.
// Create our own RangeError object.
function ourRangeError(message) {
this.name = 'OurRangeError';
this.message = message || 'Default Message Used When No Message Passed';
}
// Inherit from the RangeError prototype.
ourRangeError.prototype = new RangeError();
ourRangeError.prototype.constructor = ourRangeError;
// Throw an error.
try {
throw new ourRangeError();
} catch (e) {
alert('Our Error: ' + e.name + ' Message: ' + e.message);
}
// Throw an error and a message.
try {
throw new ourRangeError('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