Summary
Creates an error object.
Syntax
new Error([message[, fileName[, lineNumber]]])
Parameters
-
message
- Human-readable description of the error
-
fileName
- Non-standard
- The name of the file containing the code that caused the exception
-
lineNumber
- Non-standard
- The line number of the code that caused the exception
Description
Runtime errors result in new Error
objects being created and thrown.
This page documents the use of the Error
object itself and its use as a constructor function. For a list of properties and methods inherited by Error
instances, see Error.prototype.
Error types
Besides the generic Error
constructor, there are six other core error constructors in JavaScript. For client-side exceptions, see Exception Handling Statements.
- EvalError
- Creates an instance representing an error that occurs regarding the global function eval()
- RangeError
- Creates an instance representing an error that occurs when a numeric variable or parameter is outside of its valid range
- ReferenceError
- Creates an instance representing an error that occurs when de-referencing an invalid reference
- SyntaxError
- Creates an instance representing a syntax error that occurs while parsing code in eval()
- TypeError
- Creates an instance representing an error that occurs when a variable or parameter is not of a valid type
- URIError
- Creates an instance representing an error that occurs when encodeURI() or decodeURI() are passed invalid parameters
Custom Error Types
The Error object can be extended to create Error Types for your project.
// Create a new object, that prototypally inherits from the Error constructor. function MyError(message) { this.name = "MyError"; this.message = message || "Default Message"; } MyError.prototype = new Error(); MyError.prototype.constructor = MyError; try { throw new MyError(); } catch (e) { console.log(e.name); // "MyError" console.log(e.message); // "Default Message" } try { throw new MyError("custom message"); } catch (e) { console.log(e.name); // "MyError" console.log(e.message); // "custom message" }
Properties
For properties available on Error
instances, see Properties of Error instances.
- prototype
-
Allows the addition of properties to
Error
instances.
Methods
For methods available on Error
instances, see Methods of Error instances.
The global Error
object contains no methods of its own, however, it does inherit some methods through the prototype chain.
__defineGetter__, __defineSetter__, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, unwatch, watch
Error
instances
All Error
instances and instances of non-generic errors inherit from Error.prototype
. As with all constructor functions, you can use the prototype of the constructor to add properties or methods to all instances created with that constructor.
Examples
Example: Throwing a generic error
Usually you create an Error object with the intention of raising it using the throw keyword. You can handle the error using the try...catch construct:
try { throw new Error("Whoops!"); } catch (e) { alert(e.name + ": " + e.message); }
Example: Handling a specific error
this should probably be removed You can choose to handle only specific error types by testing the error type with the error's constructor property or, if you're writing for modern JavaScript engines, instanceof keyword:
try { foo.bar(); } catch (e) { if (e instanceof EvalError) { alert(e.name + ": " + e.message); } else if (e instanceof RangeError) { alert(e.name + ": " + e.message); } // ... etc }