aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/javascript/reference/global_objects/syntaxerror/index.html
blob: bddaea8370aa8627732ea1cb1d6b4da4761b2602 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
---
title: SyntaxError
slug: Web/JavaScript/Reference/Global_Objects/SyntaxError
tags:
  - Error
  - JavaScript
  - Object
  - Reference
  - SyntaxError
translation_of: Web/JavaScript/Reference/Global_Objects/SyntaxError
original_slug: Web/JavaScript/Reference/Objets_globaux/SyntaxError
---
<div>{{JSRef}}</div>

<p>L'objet <code><strong>SyntaxError</strong></code> représente une erreur qui se produit lors de l'interprétation d'un code dont la syntaxe est invalide.</p>

<h2 id="Description">Description</h2>

<p>Une exception <code>SyntaxError</code> est levée lorsque le moteur JavaScript rencontre des entités lexicales invalide ou dans un ordre invalide par rapport à la grammaire du langage.</p>

<h2 id="Syntaxe">Syntaxe</h2>

<pre class="syntaxbox"><code>new SyntaxError([<var>message</var>[, <var>nomFichier</var>[, <var>numLigne</var>]]])</code></pre>

<h3 id="Paramètres">Paramètres</h3>

<dl>
 <dt><code>message</code>{{optional_inline}}</dt>
 <dd>Une description, lisible par un humain, de l'erreur.</dd>
 <dt><code>nomFichier</code> {{optional_inline}}{{non-standard_inline}}</dt>
 <dd>Le nom du fichier contenant le code provoquant l'erreur.</dd>
 <dt><code>numLigne</code> {{optional_inline}}{{non-standard_inline}}</dt>
 <dd>Le numéro de la ligne du code qui a provoqué l'exception.</dd>
</dl>

<h2 id="Propriétés">Propriétés</h2>

<dl>
 <dt>{{jsxref("SyntaxError.prototype")}}</dt>
 <dd>Cette méthode permet d'ajouter des propriétés aux instance de <code>SyntaxError</code>.</dd>
</dl>

<h2 id="Méthodes">Méthodes</h2>

<p>L'objet global <code>SyntaxError</code> ne contient pas de méthodes directes. En revanche, il hérite de méthodes grâce à sa chaîne de prototypes.</p>

<h2 id="Instances_de_SyntaxError">Instances de <code>SyntaxError</code></h2>

<h3 id="Propriétés_2">Propriétés</h3>

<div>{{page('/fr/docs/Web/JavaScript/Reference/Objets_globaux/SyntaxError/prototype', 'Propriétés')}}</div>

<h3 id="Méthodes_2">Méthodes</h3>

<div>{{page('/fr/docs/Web/JavaScript/Reference/Objets_globaux/SyntaxError/prototype', 'Méthodes')}}</div>

<h2 id="Exemples">Exemples</h2>

<h3 id="Intercepter_une_exception_SyntaxError">Intercepter une exception <code>SyntaxError</code></h3>

<pre class="brush: js">try {
  eval('toto truc');
} catch (e) {
  console.log(e instanceof SyntaxError); // true
  console.log(e.message);                // "missing ; before statement"
  console.log(e.name);                   // "SyntaxError"
  console.log(e.fileName);               // "Scratchpad/1"
  console.log(e.lineNumber);             // 1
  console.log(e.columnNumber);           // 4
  console.log(e.stack);                  // "@Scratchpad/1:2:3\n"
}
</pre>

<h3 id="Créer_une_exception_SyntaxError">Créer une exception <code>SyntaxError</code></h3>

<pre class="brush: js">try {
  throw new SyntaxError('Coucou', 'unFichier.js', 10);
} catch (e) {
  console.log(e instanceof SyntaxError); // true
  console.log(e.message);                // "Coucou"
  console.log(e.name);                   // "SyntaxError"
  console.log(e.fileName);               // "unFichier.js"
  console.log(e.lineNumber);             // 10
  console.log(e.columnNumber);           // 0
  console.log(e.stack);                  // "@Scratchpad/2:11:9\n"
}
</pre>

<h2 id="Spécifications">Spécifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Spécification</th>
   <th scope="col">Statut</th>
   <th scope="col">Commentaires</th>
  </tr>
  <tr>
   <td>{{SpecName('ES3')}}</td>
   <td>{{Spec2('ES3')}}</td>
   <td>Définition initiale.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.11.6.4', 'SyntaxError')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-native-error-types-used-in-this-standard-syntaxerror', 'SyntaxError')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-native-error-types-used-in-this-standard-syntaxerror', 'SyntaxError')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibilité_des_navigateurs">Compatibilité des navigateurs</h2>

<p>{{Compat("javascript.builtins.SyntaxError")}}</p>

<h2 id="Voir_aussi">Voir aussi</h2>

<ul>
 <li>{{jsxref("Error")}}</li>
 <li>{{jsxref("SyntaxError.prototype")}}</li>
</ul>