rescue *[]

Discussion in 'Ruby' started by Joel VanderWerf, Jan 29, 2004.

  1. Shouldn't these work the same?

    irb(main):012:0> begin; raise; rescue; end
    => nil
    irb(main):013:0> begin; raise; rescue *[]; end
    RuntimeError:
    from (irb):13
    Joel VanderWerf, Jan 29, 2004
    #1
    1. Advertising

  2. Joel VanderWerf

    Guest

    Hi,

    At Fri, 30 Jan 2004 08:37:10 +0900,
    Joel VanderWerf wrote:
    > Shouldn't these work the same?
    >
    > irb(main):012:0> begin; raise; rescue; end
    > => nil
    > irb(main):013:0> begin; raise; rescue *[]; end
    > RuntimeError:
    > from (irb):13


    I think rescue without arguments implies StandardError, not
    equal to no exceptions, similarly to when *[].

    --
    Nobu Nakada
    , Jan 30, 2004
    #2
    1. Advertising

  3. wrote:
    > Hi,
    >
    > At Fri, 30 Jan 2004 08:37:10 +0900,
    > Joel VanderWerf wrote:
    >
    >>Shouldn't these work the same?
    >>
    >>irb(main):012:0> begin; raise; rescue; end
    >>=> nil
    >>irb(main):013:0> begin; raise; rescue *[]; end
    >>RuntimeError:
    >> from (irb):13

    >
    >
    > I think rescue without arguments implies StandardError, not


    Agree.

    > equal to no exceptions, similarly to when *[].


    But I would say that both cases are "rescue without arguments". If I'm
    wrong, then rescue argument processing is different from method argument
    processing (there may be good reasons for that, of course):

    irb(main):001:0> def rescue_method(*args); p args; end
    => nil
    irb(main):002:0> rescue_method
    []
    => nil
    irb(main):003:0> rescue_method(*[])
    []
    => nil
    Joel VanderWerf, Jan 30, 2004
    #3
  4. Joel VanderWerf

    Guest

    Hi,

    At Fri, 30 Jan 2004 09:38:50 +0900,
    Joel VanderWerf wrote:
    > > equal to no exceptions, similarly to when *[].

    >
    > But I would say that both cases are "rescue without arguments". If I'm
    > wrong, then rescue argument processing is different from method argument
    > processing (there may be good reasons for that, of course):


    Actually, any arbitrary expression can be placed there instead
    of []. I feel an empty list means nothing should be rescued.

    --
    Nobu Nakada
    , Jan 30, 2004
    #4
  5. wrote:
    > Hi,
    >
    > At Fri, 30 Jan 2004 09:38:50 +0900,
    > Joel VanderWerf wrote:
    >
    >>>equal to no exceptions, similarly to when *[].

    >>
    >>But I would say that both cases are "rescue without arguments". If I'm
    >>wrong, then rescue argument processing is different from method argument
    >>processing (there may be good reasons for that, of course):

    >
    >
    > Actually, any arbitrary expression can be placed there instead
    > of []. I feel an empty list means nothing should be rescued.


    If the argument to rescue is an empty list, you get an error:

    irb(main):006:0> begin; raise; rescue []; end
    TypeError: class or module required for rescue clause
    from (irb):6

    If there are no arguments to rescue, you rescue StandardError:

    irb(main):007:0> begin; raise; rescue; end
    => nil

    How do you get the behavior your describe ("nothing should be rescued"),
    _except_ by using the *[] trick?

    I still don't see why *[] should behave differently for rescue than for
    methods.
    Joel VanderWerf, Jan 30, 2004
    #5
  6. Hi,

    In message "Re: rescue *[]"
    on 04/01/30, Joel VanderWerf <> writes:

    |How do you get the behavior your describe ("nothing should be rescued"),
    |_except_ by using the *[] trick?

    The *[] trick is the only way (I think) for "nothing should be
    rescued". But I guess it's OK, since virtually no one want to handle
    zero exception in rescue clause.

    |I still don't see why *[] should behave differently for rescue than for
    |methods.

    * method without argument is common.
    * rescue without capturing exception is *very* uncommon.
    * but it is still reasonable to leave *[] as nothing for
    consistency's sake.

    matz.
    Yukihiro Matsumoto, Jan 30, 2004
    #6
  7. Joel VanderWerf

    Guest

    Hi,

    At Fri, 30 Jan 2004 10:15:09 +0900,
    Joel VanderWerf wrote:
    > How do you get the behavior your describe ("nothing should be rescued"),
    > _except_ by using the *[] trick?



    irb(main):001:0> def to_be_rescued; []; end
    => nil
    irb(main):002:0> begin; raise; rescue *to_be_rescued; end
    RuntimeError:
    from (irb):2

    Here, if to_be_rescued decided nothing to be rescued, it should
    not be ignored automagically, IMHO. The method also can return
    StandardError explicitly if needed, of course.

    --
    Nobu Nakada
    , Jan 30, 2004
    #7
  8. Yukihiro Matsumoto wrote:
    > Hi,
    >
    > In message "Re: rescue *[]"
    > on 04/01/30, Joel VanderWerf <> writes:
    >
    > |How do you get the behavior your describe ("nothing should be rescued"),
    > |_except_ by using the *[] trick?
    >
    > The *[] trick is the only way (I think) for "nothing should be
    > rescued". But I guess it's OK, since virtually no one want to handle
    > zero exception in rescue clause.
    >
    > |I still don't see why *[] should behave differently for rescue than for
    > |methods.
    >
    > * method without argument is common.
    > * rescue without capturing exception is *very* uncommon.
    > * but it is still reasonable to leave *[] as nothing for
    > consistency's sake.
    >
    > matz.


    Ok, thanks for the explanations. I can easily get the behavior I want:

    class FooErr < StandardError; end
    class BarErr < StandardError; end

    def handle_exceptions(*args)
    args = [StandardError] if args.empty?
    yield
    rescue *args
    puts "handled"
    end

    handle_exceptions(FooErr, BarErr) do
    raise BarErr
    end

    handle_exceptions do
    raise
    end

    # output:
    # handled
    # handled
    Joel VanderWerf, Jan 30, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. clv
    Replies:
    0
    Views:
    352
  2. Frank Buss

    Mars Rescue Mission Challenge

    Frank Buss, Nov 19, 2004, in forum: Java
    Replies:
    8
    Views:
    358
    Surendra Singhi
    Nov 20, 2004
  3. Generic Usenet Account

    std::cin woes ---- C style macros to the rescue

    Generic Usenet Account, Dec 2, 2003, in forum: C++
    Replies:
    3
    Views:
    387
    Rob Williscroft
    Dec 2, 2003
  4. Frank Buss

    Mars Rescue Mission Challenge

    Frank Buss, Nov 19, 2004, in forum: C++
    Replies:
    1
    Views:
    311
    Howard
    Nov 19, 2004
  5. rh0dium
    Replies:
    3
    Views:
    453
    Allan Zhang
    Jan 17, 2006
Loading...

Share This Page