Project

General

Profile

Actions

Bug #20026

closed

Ruby doesn't throw a syntax error when rescuing with ||

Added by arian (Arian Faurtosh) 12 months ago. Updated 12 months ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 3.2.0 (2022-12-25 revision a528908271) [arm64-darwin23]
[ruby-core:115517]

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) 12 months ago

Actually I see what's going on here, this can be closed, not actually a ruby issue.

FooError || BarError is evaluating to FooError

Actions #2

Updated by jeremyevans0 (Jeremy Evans) 12 months ago

  • Status changed from Open to Rejected

Updated by nobu (Nobuyoshi Nakada) 12 months ago

You may want to write:

begin
  raise FooError
rescue FooError, BarError => e
  puts "rescued #{e.class}"
end
Actions

Also available in: Atom PDF

Like0
Like1Like0Like1