SyntaxError() コンストラクター
Baseline
広く利用可能
この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2015年7月以降、すべてのブラウザーで利用可能です。
SyntaxError オブジェクトは、文法的に無効なコードを解釈しようとしたときのエラーを表します。
構文
new SyntaxError([message[, fileName[, lineNumber]]])
引数
message省略可-
人間が読むためのエラーの説明です。
fileName省略可-
例外が発生したコードを含むファイルの名前です。
lineNumber省略可-
例外が発生したコードの行番号です。
例
>SyntaxError の捕捉
js
try {
eval("hoo bar");
} catch (e) {
console.error(e instanceof SyntaxError);
console.error(e.message);
console.error(e.name);
console.error(e.fileName);
console.error(e.lineNumber);
console.error(e.columnNumber);
console.error(e.stack);
}
SyntaxError の生成
js
try {
throw new SyntaxError("Hello", "someFile.js", 10);
} catch (e) {
console.error(e instanceof SyntaxError); // true
console.error(e.message); // Hello
console.error(e.name); // SyntaxError
console.error(e.fileName); // someFile.js
console.error(e.lineNumber); // 10
console.error(e.columnNumber); // 0
console.error(e.stack); // @debugger eval code:3:9
}
仕様書
| 仕様書 |
|---|
| ECMAScript® 2027 Language Specification> # sec-nativeerror-constructors> |