-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12_errors.html
More file actions
222 lines (185 loc) · 15 KB
/
12_errors.html
File metadata and controls
222 lines (185 loc) · 15 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>12. Error Handling — ArkTS Specification 1.1.0 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=b3523f8e" />
<link rel="stylesheet" type="text/css" href="_static/alabaster.css?v=039e1c02" />
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js?v=def9ab29"></script>
<script src="_static/doctools.js?v=888ff710"></script>
<script src="_static/sphinx_highlight.js?v=4825356b"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="13. Compilation Units, Packages, and Modules" href="13_modules.html" />
<link rel="prev" title="11. Enumerations" href="11_enums.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head><body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="error-handling">
<span id="id1"></span><h1><span class="section-number">12. </span>Error Handling<a class="headerlink" href="#error-handling" title="Permalink to this heading">¶</a></h1>
<p>ArkTS is designed to provide first-class support in responding to, and
recovering from different erroneous conditions in a program.</p>
<p>Two kinds of situations can occur and interrupt normal program
execution:</p>
<ul class="simple">
<li><p>Runtime errors (e.g., null pointer dereferencing, array bounds
checking, or division by zero);</p></li>
<li><p>Operation completion failures (e.g., the task of reading
and processing data from a file on disk can fail if the file does
not exist on a specified path, read permissions are not available,
or else).</p></li>
</ul>
<p id="index-0">This specification uses the terms as follows:</p>
<ul class="simple">
<li><p>“<em>error</em>” to denote runtime errors, and</p></li>
<li><p>“<em>exception</em>” to denote failures.</p></li>
</ul>
<p>The difference between these two terms is that <em>exceptions</em> are the
<em>normal</em> and expected way for an operation to complete. A program
is expected to resolve some exceptions, and inform the user if it
cannot.</p>
<p>On the contrary, <em>errors</em> indicate that there is a failure of the
program logic, or even of the hardware. The program can recover in
some but not all cases.</p>
<p>As a result, <em>exceptions</em> can be handled in a much more effective
manner than <em>errors</em>.</p>
<p id="index-1">Some modern programming languages support only <em>exceptions</em>; others
support only <em>errors</em>. ArkTS is based on the presumption that both
<em>exceptions</em> and <em>errors</em> must be supported. <em>Exception</em> and
<em>Error</em> as predefined types are discussed below.</p>
<p>Exceptions are described in the chapter Experimental Features (see
<a class="reference internal" href="17_experimental.html#exceptions"><span class="std std-ref">Exceptions</span></a>) of this specification.</p>
<div class="line-block" id="index-2">
<div class="line"><br /></div>
</div>
<section id="errors">
<h2><span class="section-number">12.1. </span>Errors<a class="headerlink" href="#errors" title="Permalink to this heading">¶</a></h2>
<p><em>Error</em> is the base class of all errors. Defining a new error class is
normally not required because error classes for various cases (e.g.,
<em>DivideByZeroError</em>) are defined in the standard library (see
<a class="reference internal" href="18_stdlib.html#standard-library"><span class="std std-ref">Standard Library</span></a>).</p>
<p>However, a developer can define a new error by using <em>Error</em>, or any
derived class as the base of the new class. An example of the <em>error</em>
handling is provided below:</p>
<div class="line-block" id="index-3">
<div class="line"><br /></div>
</div>
<div class="highlight-typescript notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="w"> </span><span class="kd">class</span><span class="w"> </span><span class="nx">DivideByZeroError</span><span class="w"> </span><span class="k">extends</span><span class="w"> </span><span class="ne">Error</span><span class="w"> </span><span class="p">{}</span>
<span class="linenos"> 2</span>
<span class="linenos"> 3</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nx">divide</span><span class="p">(</span><span class="nx">a</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="nb">Number</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 4</span><span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 5</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">b</span>
<span class="linenos"> 6</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 7</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 8</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="ow">instanceof</span><span class="w"> </span><span class="nx">DivideByZeroError</span><span class="p">)</span>
<span class="linenos"> 9</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">null</span>
<span class="linenos">10</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mf">0</span>
<span class="linenos">11</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">12</span><span class="w"> </span><span class="p">}</span>
</pre></div>
</div>
<p>A compile-time error occurs if a generic class is directly or indirectly
a subclass of <em>Error</em>.</p>
<p>In most cases, <em>errors</em> are caused by the Virtual Machine, or by the
standard libraries.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">throw</span></code> statements (see <a class="reference internal" href="8_statements.html#throw-statements"><span class="std std-ref">throw Statements</span></a>) allow throwing both
<em>exceptions</em> and <em>errors</em>. Throwing <em>exceptions</em> provide a structured way to
handle a range of unexpected situations in the application code. Throwing
<em>errors</em> in such a context is not recommended.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">try</span></code> statements (see <a class="reference internal" href="8_statements.html#try-statements"><span class="std std-ref">try Statements</span></a>) are used to handle
<em>errors</em> in a manner similar to the handling of <em>exceptions</em>.</p>
<p><strong>Note</strong>: Not every <em>error</em> can be recovered.</p>
<div class="highlight-typescript notranslate" id="index-4"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="w"> </span><span class="kd">class</span><span class="w"> </span><span class="nx">Exception</span><span class="w"> </span><span class="k">extends</span><span class="w"> </span><span class="ne">Error</span><span class="w"> </span><span class="p">{}</span>
<span class="linenos"> 2</span>
<span class="linenos"> 3</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nx">handleAll</span><span class="p">(</span>
<span class="linenos"> 4</span><span class="w"> </span><span class="nx">actions</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="ow">void</span><span class="p">,</span>
<span class="linenos"> 5</span><span class="w"> </span><span class="nx">error_handling_actions</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="ow">void</span><span class="p">,</span>
<span class="linenos"> 6</span><span class="w"> </span><span class="nx">exception_handling_actions</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="ow">void</span><span class="p">)</span>
<span class="linenos"> 7</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 8</span><span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 9</span><span class="w"> </span><span class="nx">actions</span><span class="p">()</span>
<span class="linenos">10</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">11</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">12</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="ow">instanceof</span><span class="w"> </span><span class="nx">Exception</span><span class="p">)</span>
<span class="linenos">13</span><span class="w"> </span><span class="nx">exception_handling_actions</span><span class="p">()</span>
<span class="linenos">14</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="ow">instanceof</span><span class="w"> </span><span class="ne">Error</span><span class="p">)</span>
<span class="linenos">15</span><span class="w"> </span><span class="nx">error_handling_actions</span><span class="p">()</span>
<span class="linenos">16</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">17</span><span class="w"> </span><span class="p">}</span>
</pre></div>
</div>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="index.html">ArkTS Specification</a></h1>
<h3>Navigation</h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="1_intro.html">1. Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="2_lexical.html">2. Lexical Elements</a></li>
<li class="toctree-l1"><a class="reference internal" href="3_types.html">3. Types</a></li>
<li class="toctree-l1"><a class="reference internal" href="4_names.html">4. Names, Declarations and Scopes</a></li>
<li class="toctree-l1"><a class="reference internal" href="5_generics.html">5. Generics</a></li>
<li class="toctree-l1"><a class="reference internal" href="6_conversions.html">6. Contexts and Conversions</a></li>
<li class="toctree-l1"><a class="reference internal" href="7_expressions.html">7. Expressions</a></li>
<li class="toctree-l1"><a class="reference internal" href="8_statements.html">8. Statements</a></li>
<li class="toctree-l1"><a class="reference internal" href="9_classes.html">9. Classes</a></li>
<li class="toctree-l1"><a class="reference internal" href="10_interfaces.html">10. Interfaces</a></li>
<li class="toctree-l1"><a class="reference internal" href="11_enums.html">11. Enumerations</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">12. Error Handling</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#errors">12.1. Errors</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="13_modules.html">13. Compilation Units, Packages, and Modules</a></li>
<li class="toctree-l1"><a class="reference internal" href="14_ambients.html">14. Ambient Declarations</a></li>
<li class="toctree-l1"><a class="reference internal" href="15_semantics.html">15. Semantic Rules</a></li>
<li class="toctree-l1"><a class="reference internal" href="16_gui.html">16. Support for GUI Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="17_experimental.html">17. Experimental Features</a></li>
<li class="toctree-l1"><a class="reference internal" href="18_stdlib.html">18. Standard Library</a></li>
<li class="toctree-l1"><a class="reference internal" href="19_grammar.html">19. Grammar Summary</a></li>
<li class="toctree-l1"><a class="reference internal" href="20_implementation.html">20. Implementation Details</a></li>
<li class="toctree-l1"><a class="reference internal" href="21_TS_compatibility.html">21. ArkTS-TypeScript compatibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="0_authors.html">22. Contributors</a></li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="11_enums.html" title="previous chapter"><span class="section-number">11. </span>Enumerations</a></li>
<li>Next: <a href="13_modules.html" title="next chapter"><span class="section-number">13. </span>Compilation Units, Packages, and Modules</a></li>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2021-2023 Huawei Device Co., Ltd..
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 7.1.2</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
|
<a href="_sources/12_errors.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>