20.5 Error Objects
Instances of Error objects are thrown as exceptions when runtime errors occur. The Error objects may also serve as base objects for user-defined exception classes.
When an ECMAScript implementation detects a runtime error, it throws a new instance of one of the NativeError objects defined in
20.5.1 The Error Constructor
The Error
- is %Error%.
- is the initial value of the
"Error" property of theglobal object . - creates and initializes a new Error object when called as a function rather than as a
constructor . Thus the function callError(…)
is equivalent to the object creation expressionnew Error(…)
with the same arguments. - may be used as the value of an
extends
clause of a class definition. Subclassconstructors that intend to inherit the specified Error behaviour must include asuper
call to the Errorconstructor to create and initialize subclass instances with an [[ErrorData]] internal slot.
20.5.1.1 Error ( message [ , options ] )
This function performs the following steps when called:
- If NewTarget is
undefined , let newTarget be theactive function object ; else let newTarget be NewTarget. - Let O be ?
OrdinaryCreateFromConstructor (newTarget,"%Error.prototype%" , « [[ErrorData]] »). - If message is not
undefined , then- Let msg be ?
ToString (message). - Perform
CreateNonEnumerableDataPropertyOrThrow (O,"message" , msg).
- Let msg be ?
- Perform ?
InstallErrorCause (O, options). - Return O.
20.5.2 Properties of the Error Constructor
The Error
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
20.5.2.1 Error.prototype
The initial value of Error.prototype
is the
This property has the attributes { [[Writable]]:
20.5.3 Properties of the Error Prototype Object
The Error prototype object:
- is %Error.prototype%.
- is an
ordinary object . - is not an Error instance and does not have an [[ErrorData]] internal slot.
- has a [[Prototype]] internal slot whose value is
%Object.prototype% .
20.5.3.1 Error.prototype.constructor
The initial value of Error.prototype.constructor
is
20.5.3.2 Error.prototype.message
The initial value of Error.prototype.message
is the empty String.
20.5.3.3 Error.prototype.name
The initial value of Error.prototype.name
is
20.5.3.4 Error.prototype.toString ( )
This method performs the following steps when called:
- Let O be the
this value. - If O
is not an Object , throw aTypeError exception. - Let name be ?
Get (O,"name" ). - If name is
undefined , set name to"Error" ; otherwise set name to ?ToString (name). - Let msg be ?
Get (O,"message" ). - If msg is
undefined , set msg to the empty String; otherwise set msg to ?ToString (msg). - If name is the empty String, return msg.
- If msg is the empty String, return name.
- Return the
string-concatenation of name, the code unit 0x003A (COLON), the code unit 0x0020 (SPACE), and msg.
20.5.4 Properties of Error Instances
Error instances are Object.prototype.toString
.
20.5.5 Native Error Types Used in This Standard
A new instance of one of the NativeError objects below or of the AggregateError object is thrown when a runtime error is detected. All NativeError objects share the same structure, as described in
20.5.5.1 EvalError
The EvalError
This exception is not currently used within this specification. This object remains for compatibility with previous editions of this specification.
20.5.5.2 RangeError
The RangeError
Indicates a value that is not in the set or range of allowable values.
20.5.5.3 ReferenceError
The ReferenceError
Indicate that an invalid reference has been detected.
20.5.5.4 SyntaxError
The SyntaxError
Indicates that a parsing error has occurred.
20.5.5.5 TypeError
The TypeError
TypeError is used to indicate an unsuccessful operation when none of the other NativeError objects are an appropriate indication of the failure cause.
20.5.5.6 URIError
The URIError
Indicates that one of the global URI handling functions was used in a way that is incompatible with its definition.
20.5.6 NativeError Object Structure
When an ECMAScript implementation detects a runtime error, it throws a new instance of one of the NativeError objects defined in
For each error object, references to NativeError in the definition should be replaced with the appropriate error object name from
20.5.6.1 The NativeError Constructors
Each NativeError
- creates and initializes a new NativeError object when called as a function rather than as a
constructor . A call of the object as a function is equivalent to calling it as aconstructor with the same arguments. Thus the function callNativeError(…)
is equivalent to the object creation expressionnew NativeError(…)
with the same arguments. - may be used as the value of an
extends
clause of a class definition. Subclassconstructors that intend to inherit the specified NativeError behaviour must include asuper
call to the NativeErrorconstructor to create and initialize subclass instances with an [[ErrorData]] internal slot.
20.5.6.1.1 NativeError ( message [ , options ] )
Each NativeError function performs the following steps when called:
- If NewTarget is
undefined , let newTarget be theactive function object ; else let newTarget be NewTarget. - Let O be ?
OrdinaryCreateFromConstructor (newTarget,"%NativeError.prototype%"
, « [[ErrorData]] »). - If message is not
undefined , then- Let msg be ?
ToString (message). - Perform
CreateNonEnumerableDataPropertyOrThrow (O,"message" , msg).
- Let msg be ?
- Perform ?
InstallErrorCause (O, options). - Return O.
The actual value of the string passed in step
20.5.6.2 Properties of the NativeError Constructors
Each NativeError
- has a [[Prototype]] internal slot whose value is
%Error% . - has a
"name" property whose value is the String value"NativeError" . - has the following properties:
20.5.6.2.1 NativeError.prototype
The initial value of NativeError.prototype
is a NativeError prototype object (
This property has the attributes { [[Writable]]:
20.5.6.3 Properties of the NativeError Prototype Objects
Each NativeError prototype object:
- is an
ordinary object . - is not an Error instance and does not have an [[ErrorData]] internal slot.
- has a [[Prototype]] internal slot whose value is
%Error.prototype% .
20.5.6.3.1 NativeError.prototype.constructor
The initial value of the
20.5.6.3.2 NativeError.prototype.message
The initial value of the
20.5.6.3.3 NativeError.prototype.name
The initial value of the
20.5.6.4 Properties of NativeError Instances
NativeError instances are Object.prototype.toString
(
20.5.7 AggregateError Objects
20.5.7.1 The AggregateError Constructor
The AggregateError
- is %AggregateError%.
- is the initial value of the
"AggregateError" property of theglobal object . - creates and initializes a new AggregateError object when called as a function rather than as a
constructor . Thus the function callAggregateError(…)
is equivalent to the object creation expressionnew AggregateError(…)
with the same arguments. - may be used as the value of an
extends
clause of a class definition. Subclassconstructors that intend to inherit the specified AggregateError behaviour must include asuper
call to the AggregateErrorconstructor to create and initialize subclass instances with an [[ErrorData]] internal slot.
20.5.7.1.1 AggregateError ( errors, message [ , options ] )
This function performs the following steps when called:
- If NewTarget is
undefined , let newTarget be theactive function object ; else let newTarget be NewTarget. - Let O be ?
OrdinaryCreateFromConstructor (newTarget,"%AggregateError.prototype%" , « [[ErrorData]] »). - If message is not
undefined , then- Let msg be ?
ToString (message). - Perform
CreateNonEnumerableDataPropertyOrThrow (O,"message" , msg).
- Let msg be ?
- Perform ?
InstallErrorCause (O, options). - Let errorsList be ?
IteratorToList (?GetIterator (errors,sync )). - Perform !
DefinePropertyOrThrow (O,"errors" , PropertyDescriptor { [[Configurable]]:true , [[Enumerable]]:false , [[Writable]]:true , [[Value]]:CreateArrayFromList (errorsList) }). - Return O.
20.5.7.2 Properties of the AggregateError Constructor
The AggregateError
- has a [[Prototype]] internal slot whose value is
%Error% . - has the following properties:
20.5.7.2.1 AggregateError.prototype
The initial value of AggregateError.prototype
is
This property has the attributes { [[Writable]]:
20.5.7.3 Properties of the AggregateError Prototype Object
The AggregateError prototype object:
- is %AggregateError.prototype%.
- is an
ordinary object . - is not an Error instance or an AggregateError instance and does not have an [[ErrorData]] internal slot.
- has a [[Prototype]] internal slot whose value is
%Error.prototype% .
20.5.7.3.1 AggregateError.prototype.constructor
The initial value of AggregateError.prototype.constructor
is
20.5.7.3.2 AggregateError.prototype.message
The initial value of AggregateError.prototype.message
is the empty String.
20.5.7.3.3 AggregateError.prototype.name
The initial value of AggregateError.prototype.name
is
20.5.7.4 Properties of AggregateError Instances
AggregateError instances are Object.prototype.toString
(
20.5.8 Abstract Operations for Error Objects
20.5.8.1 InstallErrorCause ( O, options )
The abstract operation InstallErrorCause takes arguments O (an Object) and options (an
- If options
is an Object and ?HasProperty (options,"cause" ) istrue , then- Let cause be ?
Get (options,"cause" ). - Perform
CreateNonEnumerableDataPropertyOrThrow (O,"cause" , cause).
- Let cause be ?
- Return
unused .