Error
对象被抛出当发生运行时错误时。
Error
object can also be used as a base object for user-defined exceptions. See below for standard built-in error types.
Runtime errors result in new
Error
objects being created and thrown.
Besides the generic
Error
constructor, there are seven other core error constructors in JavaScript. For client-side exceptions, see
Exception handling statements
.
EvalError
eval()
.
InternalError
Creates an instance representing an error that occurs when an internal error in the JavaScript engine is thrown. E.g. "too much recursion".
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.
TypeError
Creates an instance representing an error that occurs when a variable or parameter is not of a valid type.
URIError
encodeURI()
or
decodeURI()
are passed invalid parameters.
Error()
Error
对象。
Error.captureStackTrace()
stack
property on an Error instance.
Error.prototype.message
Error message.
Error.prototype.name
Error name.
Error.prototype.description
message
.
Error.prototype.number
A non-standard Microsoft property for an error number.
Error.prototype.fileName
A non-standard Mozilla property for the path to the file that raised this error.
Error.prototype.lineNumber
A non-standard Mozilla property for the line number in the file that raised this error.
Error.prototype.columnNumber
A non-standard Mozilla property for the column number in the line that raised this error.
Error.prototype.stack
A non-standard Mozilla property for a stack trace.
Error.prototype.toString()
Object.prototype.toString()
方法。
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) {
console.error(e.name + ': ' + e.message)
}
You can choose to handle only specific error types by testing the error type with the error's
构造函数
property or, if you're writing for modern JavaScript engines,
instanceof
keyword:
try {
foo.bar()
} catch (e) {
if (e instanceof EvalError) {
console.error(e.name + ': ' + e.message)
} else if (e instanceof RangeError) {
console.error(e.name + ': ' + e.message)
}
// ... etc
}
You might want to define your own error types deriving from
Error
to be able to
throw new MyError()
and use
instanceof MyError
to check the kind of error in the exception handler. This results in cleaner and more consistent error handling code.
见 "What's a good way to extend Error in JavaScript?" on StackOverflow for an in-depth discussion.
Versions of Babel prior to 7 can handle
CustomError
class methods, but only when they are declared with
Object.defineProperty()
. Otherwise, old versions of Babel and other transpilers will not correctly handle the following code without
additional configuration
.
Some browsers include the
CustomError
构造函数在堆栈跟踪当使用 ES2015 类时。
class CustomError extends Error {
constructor(foo = 'bar', ...params) {
// Pass remaining arguments (including vendor specific ones) to parent constructor
super(...params)
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, CustomError)
}
this.name = 'CustomError'
// Custom debugging information
this.foo = foo
this.date = new Date()
}
}
try {
throw new CustomError('baz', 'bazMessage')
} catch(e) {
console.error(e.name) //CustomError
console.error(e.foo) //baz
console.error(e.message) //bazMessage
console.error(e.stack) //stacktrace
}
所有
browsers include the
CustomError
constructor in the stack trace when using a prototypal declaration.
function CustomError(foo, message, fileName, lineNumber) {
var instance = new Error(message, fileName, lineNumber);
instance.name = 'CustomError';
instance.foo = foo;
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
if (Error.captureStackTrace) {
Error.captureStackTrace(instance, CustomError);
}
return instance;
}
CustomError.prototype = Object.create(Error.prototype, {
constructor: {
value: Error,
enumerable: false,
writable: true,
configurable: true
}
});
if (Object.setPrototypeOf){
Object.setPrototypeOf(CustomError, Error);
} else {
CustomError.__proto__ = Error;
}
try {
throw new CustomError('baz', 'bazMessage');
} catch(e){
console.error(e.name); //CustomError
console.error(e.foo); //baz
console.error(e.message); //bazMessage
}
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Error' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Error
|
Chrome 1 | Edge 12 | Firefox 1 | IE 6 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
Error()
构造函数
|
Chrome 1 | Edge 12 | Firefox 1 | IE 6 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
columnNumber
非标
|
Chrome No | Edge No | Firefox 1 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android 4 | Opera Android No | Safari iOS No | Samsung Internet Android No | nodejs No |
fileName
非标
|
Chrome No | Edge No | Firefox 1 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android 4 | Opera Android No | Safari iOS No | Samsung Internet Android No | nodejs No |
lineNumber
非标
|
Chrome No | Edge No | Firefox 1 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android 4 | Opera Android No | Safari iOS No | Samsung Internet Android No | nodejs No |
message
|
Chrome 1 | Edge 12 | Firefox 1 | IE 6 | Opera 5 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
name
|
Chrome 1 | Edge 12 | Firefox 1 | IE 6 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
stack
非标
|
Chrome 3 | Edge 12 | Firefox 1 | IE 10 | Opera 10.5 | Safari 6 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 4 | Opera Android 11 | Safari iOS 6 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
toSource
非标
|
Chrome No | Edge No |
Firefox
1 — 74
|
IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android 4 | Opera Android No | Safari iOS No | Samsung Internet Android No | nodejs No |
toString
|
Chrome 1 | Edge 12 | Firefox 1 | IE 6 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
不支持
非标。预期跨浏览器支持较差。
见实现注意事项。
Error
EvalError
InternalError
RangeError
ReferenceError
SyntaxError
TypeError
URIError
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()