File: C:/Ruby27-x64/share/doc/ruby/html/syntax/exceptions_rdoc.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>exceptions - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "../";
var index_rel_prefix = "../";
</script>
<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>
<link href="../css/fonts.css" rel="stylesheet">
<link href="../css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="file">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="../index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="../table_of_contents.html#pages">Pages</a>
<a href="../table_of_contents.html#classes">Classes</a>
<a href="../table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="project-metadata">
<div id="fileindex-section" class="nav-section">
<h3>Pages</h3>
<ul class="link-list">
<li><a href="../CONTRIBUTING_md.html">CONTRIBUTING</a>
<li><a href="../COPYING.html">COPYING</a>
<li><a href="../COPYING_ja.html">COPYING.ja</a>
<li><a href="../LEGAL.html">LEGAL</a>
<li><a href="../NEWS.html">NEWS</a>
<li><a href="../NEWS-1_8_7.html">NEWS-1.8.7</a>
<li><a href="../NEWS-1_9_1.html">NEWS-1.9.1</a>
<li><a href="../NEWS-1_9_2.html">NEWS-1.9.2</a>
<li><a href="../NEWS-1_9_3.html">NEWS-1.9.3</a>
<li><a href="../NEWS-2_0_0.html">NEWS-2.0.0</a>
<li><a href="../NEWS-2_1_0.html">NEWS-2.1.0</a>
<li><a href="../NEWS-2_2_0.html">NEWS-2.2.0</a>
<li><a href="../NEWS-2_3_0.html">NEWS-2.3.0</a>
<li><a href="../NEWS-2_4_0.html">NEWS-2.4.0</a>
<li><a href="../NEWS-2_5_0.html">NEWS-2.5.0</a>
<li><a href="../NEWS-2_6_0.html">NEWS-2.6.0</a>
<li><a href="../README_ja_md.html">README.ja</a>
<li><a href="../README_md.html">README</a>
<li><a href="../bug_triaging_rdoc.html">bug_triaging</a>
<li><a href="../contributing_rdoc.html">contributing</a>
<li><a href="../contributors_rdoc.html">contributors</a>
<li><a href="../dtrace_probes_rdoc.html">dtrace_probes</a>
<li><a href="../extension_ja_rdoc.html">extension.ja</a>
<li><a href="../extension_rdoc.html">extension</a>
<li><a href="../globals_rdoc.html">globals</a>
<li><a href="../keywords_rdoc.html">keywords</a>
<li><a href="../lib/racc/pre-setup.html">pre-setup</a>
<li><a href="../lib/racc/rdoc/grammar_en_rdoc.html">grammar.en</a>
<li><a href="../maintainers_rdoc.html">maintainers</a>
<li><a href="../marshal_rdoc.html">marshal</a>
<li><a href="../regexp_rdoc.html">regexp</a>
<li><a href="../security_rdoc.html">security</a>
<li><a href="../signals_rdoc.html">signals</a>
<li><a href="../standard_library_rdoc.html">standard_library</a>
<li><a href="../syntax_rdoc.html">syntax</a>
<li><a href="../syntax/assignment_rdoc.html">assignment</a>
<li><a href="../syntax/calling_methods_rdoc.html">calling_methods</a>
<li><a href="../syntax/comments_rdoc.html">comments</a>
<li><a href="../syntax/control_expressions_rdoc.html">control_expressions</a>
<li><a href="../syntax/exceptions_rdoc.html">exceptions</a>
<li><a href="../syntax/literals_rdoc.html">literals</a>
<li><a href="../syntax/methods_rdoc.html">methods</a>
<li><a href="../syntax/miscellaneous_rdoc.html">miscellaneous</a>
<li><a href="../syntax/modules_and_classes_rdoc.html">modules_and_classes</a>
<li><a href="../syntax/precedence_rdoc.html">precedence</a>
<li><a href="../syntax/refinements_rdoc.html">refinements</a>
<li><a href="../win32/README_win32.html">README.win32</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-label="Page syntax/exceptions.rdoc">
<h1 id="label-Exception+Handling"><a href="../Exception.html"><code>Exception</code></a> Handling<span><a href="#label-Exception+Handling">¶</a> <a href="#top">↑</a></span></h1>
<p>Exceptions are rescued in a <code>begin</code>/<code>end</code> block:</p>
<pre class="ruby"><span class="ruby-keyword">begin</span>
<span class="ruby-comment"># code that might raise</span>
<span class="ruby-keyword">rescue</span>
<span class="ruby-comment"># handle exception</span>
<span class="ruby-keyword">end</span>
</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="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-keyword">rescue</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-keyword">end</span>
</pre>
<p>The same is true for a <code>class</code>, <code>module</code>, and <code>block</code>:</p>
<pre class="ruby">[<span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">2</span>].<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
<span class="ruby-value">10</span> <span class="ruby-operator">/</span> <span class="ruby-identifier">i</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">ZeroDivisionError</span>
<span class="ruby-keyword">nil</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment">#=> [nil, 10, 5]</span>
</pre>
<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="ruby"><span class="ruby-keyword">begin</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-operator">=></span> <span class="ruby-identifier">exception</span>
<span class="ruby-identifier">warn</span> <span class="ruby-identifier">exception</span>.<span class="ruby-identifier">message</span>
<span class="ruby-identifier">raise</span> <span class="ruby-comment"># re-raise the current exception</span>
<span class="ruby-keyword">end</span>
</pre>
<p>By default, <a href="../StandardError.html"><code>StandardError</code></a> 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="ruby"><span class="ruby-keyword">begin</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-constant">NameError</span>
<span class="ruby-comment"># handle ArgumentError or NameError</span>
<span class="ruby-keyword">end</span>
</pre>
<p>You may rescue different types of exceptions in different ways:</p>
<pre class="ruby"><span class="ruby-keyword">begin</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">ArgumentError</span>
<span class="ruby-comment"># handle ArgumentError</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">NameError</span>
<span class="ruby-comment"># handle NameError</span>
<span class="ruby-keyword">rescue</span>
<span class="ruby-comment"># handle any StandardError</span>
<span class="ruby-keyword">end</span>
</pre>
<p>The exception is matched to the rescue section starting at the top, and matches only once. If an <a href="../ArgumentError.html"><code>ArgumentError</code></a> is raised in the begin section, it will not be handled in the <a href="../StandardError.html"><code>StandardError</code></a> section.</p>
<p>You may retry rescued exceptions:</p>
<pre class="ruby"><span class="ruby-keyword">begin</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-keyword">rescue</span>
<span class="ruby-comment"># do something that may change the result of the begin block</span>
<span class="ruby-keyword">retry</span>
<span class="ruby-keyword">end</span>
</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 <a href="../SyntaxError.html"><code>SyntaxError</code></a>. If you wish to retry a block iteration use <code>redo</code>. See <a href="control_expressions_rdoc.html">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="ruby"><span class="ruby-keyword">begin</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-keyword">rescue</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-keyword">ensure</span>
<span class="ruby-comment"># this always runs</span>
<span class="ruby-keyword">end</span>
</pre>
<p>You may also run some code when an exception is not raised:</p>
<pre class="ruby"><span class="ruby-keyword">begin</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-keyword">rescue</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-keyword">else</span>
<span class="ruby-comment"># this runs only when no exception was raised</span>
<span class="ruby-keyword">ensure</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-keyword">end</span>
</pre>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>