[ new ] ~Error【コンストラクタ】
toString【文字列変換】
[ new ] AggregateError【複数エラー コンストラクタ】
[ new ] ~Error【コンストラクタ】
メモ
概要
- ~Error【各種エラー】オブジェクトを生成
- エラー種類:~Error【各種エラー】オブジェクト
外部リンク
- ECMA-262 (英語)
The Error Constructor ES2024 (15) ES2023 (14) ES2022 (13)
構文
[new] ~Error( message[, options ] )
[new] ~Error( [ number[, description]] )
[new] ~Error( [ message[, fileName[, lineNumber]]] )
各種 Error オブジェクト
message エラーメッセージ (文字列変換)
options (Object) プロパティ オプション
cause: エラー原因
number エラー番号
description エラー説明
fileName エラー ファイル パス名
lineNumber エラー ファイル行番号
例
try {
throw new Error("Error1");
} catch (e) {
console.log(e.name, e.message);
// 出力:Error Error1
}
try {
throw Error("Error2");
} catch (e) {
console.log(e.name, e.message);
// 出力:Error Error2
}
try {
throw new SyntaxError("[SyntaxError]");
} catch (e) {
console.log(e.name, e.message);
// 出力:SyntaxError [SyntaxError]
}
try {
try {
try {
throw Error("Error message X");
} catch (e) {
throw Error("Error message Y");
}
} catch (e) {
throw Error("Error message Z");
}
} catch (e) {
console.log(e);
// 出力例:
// Error: Error message Z
// ~
}
try {
try {
try {
throw Error("Error message X");
} catch (e) {
throw Error("Error message Y", { cause: e });
}
} catch (e) {
throw Error("Error message Z", { cause: e });
}
} catch (e) {
console.log(e);
// 出力例:
// Error: Error message Z
// ~
// Caused by: Error: Error message Y
// ~
// Caused by: Error: Error message X
// ~
}
Error.prototype.toString【文字列変換】
メモ
概要
- エラーを文字列に変換
外部リンク
- ECMA-262 (英語)
Error.prototype.toString ( ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
Error.toString()
表現文字列
例
try {
let num = 123;
let str = num.toString(0);
} catch (e) {
console.log(e.name);
// 出力:RangeError
console.log(e.message);
// 出力:toString() radix argument must be between 2 and 36
console.log(e.toString());
// 出力:RangeError: toString() radix argument must be between 2 and 36
}
[ new ] AggregateError【複数エラー コンストラクタ】
メモ
概要
- AggregateError【各種エラー】オブジェクトを生成
- サブクラス化可能
外部リンク
- ECMA-262 (英語)
The AggregateError Constructor ES2024 (15) ES2023 (14) ES2022 (13)
構文
[new] AggregateError(errors[, message] )
AggregateError オブジェクト
errors エラーのイテラブルオブジェクト
message エラーメッセージ
例
try {
throw new AggregateError(
[
new Error("Error1"),
new Error("Error2"),
new Error("Error3"),
],
'ERROR');
} catch (e) {
console.log(e.name);
// 出力:AggregateError
console.log(e.message);
// 出力:ERROR
console.log(e.errors);
// 出力:
// Array(3) [ Error, Error, Error ]
// 0: Error: Error1
// 1: Error: Error2
// 2: Error: Error3
// length: 3
}