Professional Web Applications Themes

rescue *[] - Ruby

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...

  1. #1

    Default rescue *[]


    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 Guest

  2. #2

    Default Re: rescue *[]

    Hi,

    At Fri, 30 Jan 2004 08:37:10 +0900,
    Joel VanderWerf wrote: 

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

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  3. #3

    Default Re: rescue *[]

    net wrote: 
    >
    >
    > I think rescue without arguments implies StandardError, not[/ref]

    Agree.
     

    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 Guest

  4. #4

    Default Re: rescue *[]

    Hi,

    At Fri, 30 Jan 2004 09:38:50 +0900,
    Joel VanderWerf wrote: 
    >
    > 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):[/ref]

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

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  5. #5

    Default Re: rescue *[]

    net wrote: 
    >>
    >>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):[/ref]
    >
    >
    > Actually, any arbitrary expression can be placed there instead
    > of []. I feel an empty list means nothing should be rescued.[/ref]

    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 Guest

  6. #6

    Default Re: rescue *[]

    Hi,

    In message "Re: rescue *[]"
    on 04/01/30, Joel VanderWerf <Berkeley.EDU> 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 Guest

  7. #7

    Default Re: rescue *[]

    Hi,

    At Fri, 30 Jan 2004 10:15:09 +0900,
    Joel VanderWerf wrote: 


    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


    nobu.nokada@softhome.net Guest

  8. #8

    Default Re: rescue *[]

    Yukihiro Matsumoto wrote: 

    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 Guest

Similar Threads

  1. =~ and rescue
    By Patrick Gundlach in forum Ruby
    Replies: 13
    Last Post: October 23rd, 10:24 PM
  2. Please rescue me from OLE DB Hell
    By oj in forum Microsoft SQL / MS SQL Server
    Replies: 2
    Last Post: July 23rd, 05:54 AM
  3. rescue without floppy or CD?
    By Greg Balls in forum Debian
    Replies: 1
    Last Post: July 11th, 06:30 PM
  4. Rescue CD doesn't boot on Acer TM 800 after deleting rescue partition
    By Rene Lanz in forum Linux Setup, Configuration & Administration
    Replies: 2
    Last Post: July 8th, 02:27 AM
  5. Bootable Rescue CD-ROM
    By Sebastian Kapfer in forum Debian
    Replies: 8
    Last Post: July 5th, 12:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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