-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile.exceptions.html
155 lines (125 loc) · 6.58 KB
/
file.exceptions.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no'>
<meta name='apple-touch-fullscreen' content='yes'>
<meta name='apple-mobile-web-app-capable' content='yes'>
<meta name='apple-mobile-web-app-status-bar-style' content='rgba(228,228,228,1.0)'>
<title>File: Exceptions — Ruby-2.6.10</title>
<link rel='stylesheet' type='text/css' href='../css/y_fonts.css' />
<link rel='stylesheet' type='text/css' href='../css/highlight.github.css' />
<link rel='stylesheet' type='text/css' href='../css/y_style.css' />
<link rel='stylesheet' type='text/css' href='../css/y_list.css' />
<link rel='stylesheet' type='text/css' href='../css/y_color.css' />
<script type='text/javascript'>
var pathId = "exceptions",
relpath = '';
var t2Info = {
CSEP: '.',
ISEP: '#',
NSEP: '::'
};
</script>
<script type='text/javascript' charset='utf-8' src='../js/highlight.pack.js'></script>
<script type='text/javascript' charset='utf-8' src='../js/y_app.js'></script>
</head>
<body>
<svg id='y_wait' class viewBox='0 0 90 90'></svg>
<div id='settings' class='hidden'></div>
<div id='y_list' class='d h'>
<header id='list_header'></header>
<nav id= 'list_nav' class='y_nav l_nav'>
<ul id='list_items'></ul>
</nav>
</div>
<div id='y_toc' class='f h'>
<header id='toc_header'></header>
<nav id= 'toc_nav' class='y_nav t_nav'>
<ol id='toc_items'></ol>
</nav>
</div>
<div id='y_main' tabindex='-1'>
<header id='y_header'>
<div id='y_menu'>
<a id='home_no_xhr' href='/'>Home</a> »
<a href='.'>Ruby-2.6.10</a> »
<a href='_index.html'>Index</a> »
<span class='title'><a id='t2_doc_top' href='#'>File: Exceptions ▲</a></span>
</div>
<a id='list_href' href="file_list.html"></a>
<div id='y_measure_em' class='y_measure'></div>
<div id='y_measure_vh' class='y_measure'></div>
<span id='y_measure_50pre' class='y_measure'><code>123456789_123456789_123456789_123456789_123456789_</code></span>
</header>
<div id='content' class='file'>
<h1 id="label-Exception+Handling">Exception Handling</h1>
<p>Exceptions are rescued in a <code>begin</code>/<code>end</code> block:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>begin</span>
<span class='comment'># code that might raise
</span><span class='kw'>rescue</span>
<span class='comment'># handle exception
</span><span class='kw'>end</span></code></pre>
<p>If you are inside a method, you do not need to use <code>begin</code> or <code>end</code> unless you wish to limit the scope of rescued exceptions:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>def</span> <span class='id identifier rubyid_my_method'>my_method</span>
<span class='comment'># ...
</span><span class='kw'>rescue</span>
<span class='comment'># ...
</span><span class='kw'>end</span></code></pre>
<p>The same is true for a <code>class</code> or <code>module</code>.</p>
<p>You can assign the exception to a local variable by using <code>=> variable_name</code> at the end of the <code>rescue</code> line:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>begin</span>
<span class='comment'># ...
</span><span class='kw'>rescue</span> <span class='op'>=></span> <span class='id identifier rubyid_exception'>exception</span>
<span class='id identifier rubyid_warn'>warn</span> <span class='id identifier rubyid_exception'>exception</span>.<span class='id identifier rubyid_message'>message</span>
<span class='id identifier rubyid_raise'>raise</span> <span class='comment'># re-raise the current exception
</span><span class='kw'>end</span></code></pre>
<p>By default, StandardError and its subclasses are rescued. You can rescue a specific set of exception classes (and their subclasses) by listing them after <code>rescue</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>begin</span>
<span class='comment'># ...
</span><span class='kw'>rescue</span> <span class='const'>ArgumentError</span><span class='comma'>,</span> <span class='const'>NameError</span>
<span class='comment'># handle ArgumentError or NameError
</span><span class='kw'>end</span></code></pre>
<p>You may rescue different types of exceptions in different ways:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>begin</span>
<span class='comment'># ...
</span><span class='kw'>rescue</span> <span class='const'>ArgumentError</span>
<span class='comment'># handle ArgumentError
</span><span class='kw'>rescue</span> <span class='const'>NameError</span>
<span class='comment'># handle NameError
</span><span class='kw'>rescue</span>
<span class='comment'># handle any StandardError
</span><span class='kw'>end</span></code></pre>
<p>The exception is matched to the rescue section starting at the top, and matches only once. If an ArgumentError is raised in the begin section, it will not be handled in the StandardError section.</p>
<p>You may retry rescued exceptions:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>begin</span>
<span class='comment'># ...
</span><span class='kw'>rescue</span>
<span class='comment'># do something that may change the result of the begin block
</span> <span class='kw'>retry</span>
<span class='kw'>end</span></code></pre>
<p>Execution will resume at the start of the begin block, so be careful not to create an infinite loop.</p>
<p>Inside a rescue block is the only valid location for <code>retry</code>, all other uses will raise a SyntaxError. If you wish to retry a block iteration use <code>redo</code>. See <a href="file.control_expressions.html" title="Control Expressions">Control Expressions</a> for details.</p>
<p>To always run some code whether an exception was raised or not, use <code>ensure</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>begin</span>
<span class='comment'># ...
</span><span class='kw'>rescue</span>
<span class='comment'># ...
</span><span class='kw'>ensure</span>
<span class='comment'># this always runs
</span><span class='kw'>end</span></code></pre>
<p>You may also run some code when an exception is not raised:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>begin</span>
<span class='comment'># ...
</span><span class='kw'>rescue</span>
<span class='comment'># ...
</span><span class='kw'>else</span>
<span class='comment'># this runs only when no exception was raised
</span><span class='kw'>ensure</span>
<span class='comment'># ...
</span><span class='kw'>end</span></code></pre>
<div id='footer'></div>
</div> <!-- content -->
</div> <!-- y_main -->
</body>
</html>