--- title: Error slug: Web/JavaScript/Reference/Global_Objects/Error tags: - Error - JavaScript - Reference translation_of: Web/JavaScript/Reference/Global_Objects/Error ---
Error オブジェクトは、実行時エラーが発生した時に発生します。 Error オブジェクトは、ユーザー定義の例外の基底オブジェクトとして使用することもできます。標準の組み込みエラー型については下記を参照してください。
実行時エラーが発生すると、新しい Error オブジェクトが生成されスローされます。
JavaScript には、一般的な Error コンストラクターの他に、中核となる 7 つのエラーコンストラクターがあります。クライアント側の例外については、例外処理文を参照してください。
Error オブジェクトを生成します。通常、{{JSxRef("Statements/throw", "throw")}} キーワードを使い意図的にエラーを発生させて Error オブジェクトを生成します。 {{JSxRef("Statements/try...catch", "try...catch")}} 構文を使用してエラーを処理してください:
try {
throw new Error('Whoops!')
} catch (e) {
console.error(e.name + ': ' + e.message)
}
エラーの {{JSxRef("Object.prototype.constructor", "constructor")}} プロパティでエラー型をテストすることにより、特定のエラー型だけを選んで処理できます。または、最近の JavaScript エンジン向けに書いているのであれば、{{JSxRef("Operators/instanceof", "instanceof")}} キーワードが使えます:
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
}
Error から派生した独自のエラー型を定義して throw new CustomError() ができるようにし、instanceof CustomError で例外ハンドラー内のエラーの種類を確認したいでしょう。これを行う一般的な方法の実例を以下に示します。
StackOverflow の突っ込んだ議論、 "What's a good way to extend Error in JavaScript?" も参照してください。
Babel 7 以前では独自のエラークラスのメソッドを使用することができますが、 Object.defineProperty() で宣言された場合に限定されます。 そうでない場合、古い Babel やそれ以外のトランスパイラは、下記のコードを正しく処理するために追加の設定を必要とします。
ES2015 クラスを使用した場合、一部のブラウザのスタックトレース上に、 CustomError コンストラクタが含まれます。
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
}
すべてのブラウザーのスタックトレース上に、 CustomError コンストラクターが含まれます。
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
}
| 仕様書 |
|---|
| {{SpecName('ESDraft', '#sec-error-objects', 'Error')}} |
{{Compat("javascript.builtins.Error")}}