Project

General

Profile

Feature #12623

rescue in blocks without begin/end

Added by Nondv (Dmitriy Non) over 1 year ago. Updated over 1 year ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:76550]

Description

Hi there!

There's pretty nice feature that we can use "short rescue" in method definitions:

def my_method
  raise '1234'
rescue
   puts 'rescued'
end

What about using this in blocks?

loop do
  n = enumerator.next
  # do something
rescue StopIteration
end

P.S. actually I am not sure if this FR was not created earlier but I couldn't google it.
P.P.S sorry for my english


Related issues

Is duplicate of Ruby trunk - Feature #7882: Allow rescue/else/ensure in do..endClosed
Has duplicate Ruby trunk - Feature #12906: do/end blocks work with ensure/rescue/elseClosed

History

#1 [ruby-core:76551] Updated by Nondv (Dmitriy Non) over 1 year ago

  • Description updated (diff)

#2 [ruby-core:76561] Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 1 year ago

+1, I often want this and never understood why it only worked with methods.

#3 [ruby-core:76565] Updated by nobu (Nobuyoshi Nakada) over 1 year ago

  • Description updated (diff)

AFAIK, it has been proposed a few times.

An objection is that rescue in {} block feels weird.

#4 [ruby-core:76572] Updated by Nondv (Dmitriy Non) over 1 year ago

An objection is that rescue in {} block feels weird.

Do you mean in one-line or multi-line form?

Multiline:

list.each { |x|
  # do something
rescue
  # do something
}

In single-line there's ambiguous case:

# not implemented
list.each { |x| action_1; action_2; rescue; 'error!' }

# this is valid
list.each { |x| action_1; action_2 rescue  'error!' }

But 2nd line looks bad anyway :(

So, what's problem of "feels weird"?
IMHO there're not many people that will use it like that (my first example in single-line).

#5 [ruby-core:76582] Updated by shyouhei (Shyouhei Urabe) over 1 year ago

Dmitriy Non wrote:

list.each { |x|
  # do something
rescue
  # do something
}

-1. This is odd. I cannot remember any other language syntax that goes likes this. Java, C++, C# and all other language that use {} as block notations share this syntax to write exception handlings:

static void Main()
    {
        try
        {
            // something
        }
        catch (Exception e)
        {
            // something
        }
        finally
        {
            // something
        }
    }

#6 [ruby-core:76584] Updated by Nondv (Dmitriy Non) over 1 year ago

I cannot remember any other language syntax

So, case statement in Ruby is different too.

+ it is not necessary to use this.
BTW AFAIK ruby-style-guide banned multiline {}

#7 [ruby-core:76585] Updated by duerst (Martin Dürst) over 1 year ago

Nobuyoshi Nakada wrote:

An objection is that rescue in {} block feels weird.

I feel the same way. I think it feels weird because in Ruby, program structures starting with a keyword (if/while/do/def/...) can contain keywords (else, rescue,...) and end with keywords (end), but symbols ({}, [],...) and keywords are not mixed.

This, combined with the fact that {} is mostly used single-line, may suggest that adding rescue to do blocks might work, but not for {} blocks.

#8 Updated by shyouhei (Shyouhei Urabe) over 1 year ago

  • Is duplicate of Feature #7882: Allow rescue/else/ensure in do..end added

#9 [ruby-core:76702] Updated by shyouhei (Shyouhei Urabe) over 1 year ago

  • Status changed from Open to Closed

Closing duplicated issue. Please continue discussing at Issue #7882.

#10 Updated by shyouhei (Shyouhei Urabe) about 1 year ago

  • Has duplicate Feature #12906: do/end blocks work with ensure/rescue/else added

Also available in: Atom PDF