RangeError object constructorS2C Home « Globals « 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
messageAn optional description of the error.

Class Properties

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

Instance Properties

Instance Property Description
constructorReturns a reference to the RangeError function that created the prototype.
nameAn 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);
}  

Press the button below to action the above code:

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!');
}   

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