rescue *[]

J

Joel VanderWerf

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
 
N

nobu.nokada

Hi,

At Fri, 30 Jan 2004 08:37:10 +0900,
Joel said:
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 *[].
 
J

Joel VanderWerf

Hi,

At Fri, 30 Jan 2004 08:37:10 +0900,
Joel said:
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
 
N

nobu.nokada

Hi,

At Fri, 30 Jan 2004 09:38:50 +0900,
Joel said:
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.
 
J

Joel VanderWerf

Hi,

At Fri, 30 Jan 2004 09:38:50 +0900,
Joel said:
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.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: rescue *[]"

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

nobu.nokada

Hi,

At Fri, 30 Jan 2004 10:15:09 +0900,
Joel said:
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.
 
J

Joel VanderWerf

Yukihiro said:
Hi,

In message "Re: rescue *[]"

|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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top