[ new ] ~Error()【コンストラクタ】
メモ
- ~Error【各種エラー】オブジェクトを生成
- エラー種類:~Error【各種エラー】オブジェクト
構文
Error( message )
new Error( message )
new ~Error( [ number [, description ] ] )
new ~Error( [ message [, fileName [, lineNumber ] ] ] )
戻り値各種 Error オブジェクト
messageエラーメッセージ
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]
}
関連
- ~Error【各種エラー】オブジェクト
- ECMAScript 5.1 (英語)
- ECMAScript 2015 (6) (英語)
Error.prototype.toString()【文字列変換】
メモ
- エラーを文字列に変換
構文
Error.toString()
戻り値表現文字列
例
try {
var num = 123;
var 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
}