Actions
Bug #20026
closedRuby doesn't throw a syntax error when rescuing with ||
Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 3.2.0 (2022-12-25 revision a528908271) [arm64-darwin23]
Description
We had a coworker new to ruby try an interesting syntax for rescue that doesn't result in a syntax error, and works partially.
Why does ruby allow the logical OR || operator, is there a purpose for this? If not this feels like it should be a syntax error, instead of resulting in partially working code.
class FooError < StandardError; end
class BarError < StandardError; end
# works
begin
raise FooError
rescue FooError || BarError => e
puts "rescued #{e.class}"
end
# doesn't work
begin
raise FooError
rescue FooError || BarError => e
puts "rescued #{e.class}"
end
Updated by arian (Arian Faurtosh) about 1 year ago
Actually I see what's going on here, this can be closed, not actually a ruby issue.
FooError || BarError
is evaluating to FooError
Updated by jeremyevans0 (Jeremy Evans) about 1 year ago
- Status changed from Open to Rejected
Updated by nobu (Nobuyoshi Nakada) about 1 year ago
You may want to write:
begin
raise FooError
rescue FooError, BarError => e
puts "rescued #{e.class}"
end
Actions
Like0
Like1Like0Like1