-
Notifications
You must be signed in to change notification settings - Fork 8k
/
index.md
74 lines (57 loc) · 2.67 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
---
title: SyntaxError
slug: Web/JavaScript/Reference/Global_Objects/SyntaxError
---
{{JSRef}}
**`SyntaxError`** オブジェクトは、構文的に不正なコードを解釈しようとした場合のエラーを表します。これは、 JavaScript エンジンが、コードを解析中に言語の構文に従わないトークンまたはトークンの順序に遭遇した場合に発生します。
## コンストラクター
- {{jsxref("Global_Objects/SyntaxError/SyntaxError", "SyntaxError()")}}
- : 新しい `SyntaxError` オブジェクトを生成します。
## インスタンスプロパティ
- {{jsxref("Error.prototype.message", "SyntaxError.prototype.message")}}
- : エラーメッセージです。 ECMA-262 において {{jsxref("SyntaxError")}} は自身の `message` プロパティを提供するべきとされていますが、 [SpiderMonkey](/ja/docs/Mozilla/Projects/SpiderMonkey) では {{jsxref("Error.prototype.message")}} を継承しています。
- {{jsxref("Error.prototype.name", "SyntaxError.prototype.name")}}
- : エラー名です。 {{jsxref("Error")}} から継承しています。
- {{jsxref("Error.prototype.fileName", "SyntaxError.prototype.fileName")}}
- : このエラーが発生したファイルのパスです。 {{jsxref("Error")}} から継承しています。
- {{jsxref("Error.prototype.lineNumber", "SyntaxError.prototype.lineNumber")}}
- : このエラーが発生したファイル内の行番号です。 {{jsxref("Error")}} から継承しています。
- {{jsxref("Error.prototype.columnNumber", "SyntaxError.prototype.columnNumber")}}
- : このエラーが発生した行内の桁数です。 {{jsxref("Error")}} から継承しています。
- {{jsxref("Error.prototype.stack", "SyntaxError.prototype.stack")}}
- : スタックトレースです。 {{jsxref("Error")}} から継承しています。
## 例
### 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
}
```
## 仕様書
{{Specifications}}
## ブラウザーの互換性
{{Compat}}
## 関連情報
- {{jsxref("Error")}}