https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112012-07-16T23:45:31ZRuby Issue Tracking SystemRuby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=281492012-07-16T23:45:31Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>A <code>rescue</code> clause (non-modifier) can be followed by arbitrary expressions which would return a class or a module,<br>
so the syntax in your proposal is unclear.</p>
<p>We had discussed about arguments to <code>rescue</code>-modifier when adding it, but no suitable syntax was found.</p> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=281512012-07-16T23:50:13Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/28151/diff?detail_id=20827">diff</a>)</li></ul> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=282102012-07-19T17:45:05ZQuintus (Marvin Gülker)post+rubybugs@guelker.eu
<ul></ul><blockquote>
<p>A <code>rescue</code> clause (non-modifier) can be followed by arbitrary expressions which would return a class or a module, so the syntax in your proposal is unclear.</p>
</blockquote>
<p>As said, I wasn’t happy with the syntax either, it was more about the actual <em>possibility</em> of only <code>rescue</code>ing specific exceptions.</p>
<blockquote>
<p>We had discussed about arguments to <code>rescue</code>-modifier when adding it, but no suitable syntax was found.</p>
</blockquote>
<p>Maybe it could be similar to how the <code>case</code> statement can be used. There it is possible to do:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">when</span> <span class="n">x</span> <span class="k">then</span> <span class="n">y</span>
</code></pre>
<p>So what about a syntax like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="s2">"myfile.txt"</span><span class="p">)</span> <span class="k">rescue</span> <span class="no">Errno</span><span class="o">::</span><span class="no">ENOENT</span> <span class="k">then</span> <span class="nb">puts</span> <span class="s2">"No file there"</span>
</code></pre>
<p>I.e. using the <code>then</code> keyword to distinguish the expression for the exception class from the actual rescue code.</p>
<p>Valete,<br>
Marvin</p> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=282122012-07-20T05:23:13ZAnonymous
<ul></ul><p>On Jul 19, 2012, at 3:45 AM, Quintus (Marvin Gülker) wrote:</p>
<blockquote>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="s2">"myfile.txt"</span><span class="p">)</span> <span class="k">rescue</span> <span class="no">Errno</span><span class="o">::</span><span class="no">ENOENT</span> <span class="k">then</span> <span class="nb">puts</span> <span class="s2">"No file there"</span>
</code></pre>
</blockquote>
<p>I like "when" and "then"</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="s2">"myfile.txt"</span><span class="p">)</span> <span class="k">rescue</span> <span class="k">when</span> <span class="no">Errno</span><span class="o">::</span><span class="no">ENOENT</span> <span class="k">then</span> <span class="nb">puts</span> <span class="s2">"No file there"</span>
</code></pre> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=283552012-07-24T07:41:11ZQuintus (Marvin Gülker)post+rubybugs@guelker.eu
<ul></ul><blockquote>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="s2">"myfile.txt"</span><span class="p">)</span> <span class="k">rescue</span> <span class="k">when</span> <span class="no">Errno</span><span class="o">::</span><span class="no">ENOENT</span> <span class="k">then</span> <span class="nb">puts</span> <span class="s2">"No file there"</span>
</code></pre>
</blockquote>
<p>Putting two keywords right after one another doesn’t seem like a good idea to me. If you really like this <code>when</code> there, what about this one:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="s2">"myfile.txt"</span><span class="p">)</span> <span class="k">when</span> <span class="no">Errno</span><span class="o">::</span><span class="no">ENOENT</span> <span class="k">rescue</span> <span class="nb">puts</span> <span class="s2">"No file there"</span>
</code></pre>
<p>However, this may conflict with a surrounding <code>case</code> statement. I’d prefer the <code>rescue</code>/<code>then</code> to this as it seems more clear to me.</p> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=283562012-07-24T07:43:11ZQuintus (Marvin Gülker)post+rubybugs@guelker.eu
<ul></ul><blockquote>
<p>I’d prefer the <code>rescue</code>/<code>then</code> to this as it seems more clear to me.</p>
</blockquote>
<p>Sorry, not a native speaker here... Should be "I’d prefer the <code>rescue</code>/<code>then</code> combination over this as it seems more clear to me.".</p> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=283612012-07-24T09:25:33Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>I don't think it would be possible to tell "<code>rescue</code> exceptions <code>then</code>" from mere "<code>rescue</code>", unfortunately.</p> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=283642012-07-24T09:35:54Ztrans (Thomas Sawyer)
<ul></ul><p>What about an add-"on"?</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="s2">"myfile.txt"</span><span class="p">)</span> <span class="k">rescue</span> <span class="n">on</span> <span class="no">Errno</span><span class="o">::</span><span class="no">ENOENT</span> <span class="nb">puts</span> <span class="s2">"No file there"</span>
</code></pre>
<p>Although better to read, maybe?</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="s2">"myfile.txt"</span><span class="p">)</span> <span class="k">rescue</span> <span class="nb">puts</span> <span class="s2">"No file there"</span> <span class="n">on</span> <span class="no">Errno</span><span class="o">::</span><span class="no">ENOENT</span>
</code></pre> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=284172012-07-25T13:41:19Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>New keyword is too big deal for this trivial syntax extension, I think.</p> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=317012012-10-27T07:22:31Zko1 (Koichi Sasada)
<ul><li><strong>Assignee</strong> set to <i>mame (Yusuke Endoh)</i></li></ul> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=317652012-10-27T12:02:09Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Target version</strong> changed from <i>2.0.0</i> to <i>3.0</i></li></ul> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=317952012-10-27T20:09:41Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Assignee</strong> changed from <i>mame (Yusuke Endoh)</i> to <i>matz (Yukihiro Matsumoto)</i></li></ul> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=480682014-07-26T16:22:54ZAnonymous
<ul></ul><p>How about introducing shorter "resc" keyword for error-specific inline rescue?</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">do_messy_job</span> <span class="n">resc</span> <span class="no">TypeError</span><span class="p">:</span> <span class="mi">42</span><span class="p">,</span> <span class="no">NameError</span><span class="p">:</span> <span class="mi">43</span>
</code></pre>
<p>Tbh, inline <code>rescue</code> always leaves me with a feeling that I'm doing something underhanded. Keyword "resc" would save precious 2 characters, while still being highly unique, so it could be argued that its introduction would not be too big deal. This omnivorous inline <code>rescue</code> has been really pissing me off for a long time. Together with my other proposal to officially rename <code>ArgumentError</code> to <code>ArgError</code>, "<code>resc</code>" keyword for specific inline <code>rescue</code> would be a godsend.</p> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=480862014-07-27T16:12:49Zfunny_falcon (Yura Sokolov)funny.falcon@gmail.com
<ul></ul><pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">do_something</span> <span class="k">rescue</span> <span class="no">SomeError</span> <span class="n">with</span> <span class="nb">puts</span> <span class="s2">"SomeError occured"</span>
</code></pre> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=480872014-07-27T19:28:38Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/48087/diff?detail_id=34738">diff</a>)</li></ul><p>As far as I tried, using <code>when</code> can't parse well.</p> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=495942014-10-22T23:10:18ZAnonymous
<ul></ul><p>@NobuyoshiNakada, how then about my <code>resc</code> suggestion?</p> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=496012014-10-23T05:59:26Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>No new reserved word, as possible.<br>
I don't think it is acceptable.</p> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=583762016-04-28T18:32:58ZQuintus (Marvin Gülker)post+rubybugs@guelker.eu
<ul></ul><p>I noticed we have a keyword already in Ruby that’s used nearly nowhere and could be a nice fit here. Might not be linguistically optimal, but is not too bad: <code>in</code>. That keyword is currently only used for the <code>for</code> loop and should thus be fairly clear to tell if not used in a <code>for</code> loop.</p>
<p>Suggestion 1:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="s2">"myfile.txt"</span><span class="p">)</span> <span class="k">rescue</span> <span class="no">Errno</span><span class="o">::</span><span class="no">ENOENT</span> <span class="k">in</span> <span class="nb">puts</span> <span class="s2">"No file there"</span>
</code></pre>
<p>Suggestion 2, building on top the "on" example given earlier:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="s2">"myfile.txt"</span><span class="p">)</span> <span class="k">rescue</span> <span class="nb">puts</span> <span class="s2">"No file there"</span> <span class="k">in</span> <span class="no">Errno</span><span class="o">::</span><span class="no">ENOENT</span>
</code></pre>
<p>A third suggestion, diggin up the <code>when</code> suggestion from earlier in an adapted way (like suggestion 2 above):</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="s2">"myfile.txt"</span><span class="p">)</span> <span class="k">rescue</span> <span class="nb">puts</span> <span class="s2">"No file there"</span> <span class="k">when</span> <span class="no">Errno</span><span class="o">::</span><span class="no">ENOENT</span>
</code></pre>
<p>Using <code>if</code> here is probably not possible because that would clash with the postcondition modifier.</p>
<p>Greetings<br>
Marvin</p> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=587192016-05-18T00:34:34Zjavawizard (Alex Boyd)alex@opengroove.org
<ul></ul><p>What's the problem with <code>then</code>? I'm getting a syntax error when I try it:</p>
<pre><code>irb(main):002:0> foo rescue Bar then baz
SyntaxError: (irb):2: syntax error, unexpected keyword_then, expecting end-of-input
foo rescue Bar then baz
^
from /Users/aboyd/.rbenv/versions/2.1.2/bin/irb:11:in `<main>'
</code></pre>
<p>I don't believe there's anywhere <code>then</code> can appear without something like <code>if</code> leading into it, in which case the <code>then</code> would obviously belong to the <code>if</code>, like:</p>
<pre><code>foo rescue if bar then Baz else Qux end then blah
</code></pre>
<p>(which probably isn't actually advisable, but would be perfectly syntactically unambiguous.)</p> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=587202016-05-18T00:42:02Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>We looked at it in yesterday's developer meeting. Nobu told us that all the proposed syntax so far renders conflicts to existing syntax.</p>
<p>My feeling is that rescue modifier is "too easy to use" compared to ordinary begin-rescue-end. People tend to inappropriately use modifier style because it's much shorter. I personally suggest a coding style to restrict its usage to contain no side effects; like <code>expr rescue nil</code> or <code>expr rescue false</code>.</p> Ruby master - Feature #6739: One-line rescue statement should support specifying an exception classhttps://bugs.ruby-lang.org/issues/6739?journal_id=702452018-02-07T01:14:30Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-7 priority-4 priority-default closed" href="/issues/10042">Feature #10042</a>: Deprecate postfix rescue syntax for removal in 3.0</i> added</li></ul>