Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4543676
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:33:52+00:00 2026-05-21T15:33:52+00:00

I have some code that needs to rescue multiple types of exceptions in ruby:

  • 0

I have some code that needs to rescue multiple types of exceptions in ruby:

begin
  a = rand
  if a > 0.5
    raise FooException
  else
    raise BarException
  end
rescue FooException, BarException
  puts "rescued!"
end

What I’d like to do is somehow store the list of exception types that I want to rescue somewhere and pass those types to the rescue clause:

EXCEPTIONS = [FooException, BarException]

and then:

rescue EXCEPTIONS

Is this even possible, and is it possible without some really hack-y calls to eval? I’m not hopeful given that I’m seeing TypeError: class or module required for rescue clause when I attempt the above.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:33:53+00:00Added an answer on May 21, 2026 at 3:33 pm

    You can use an array with the splat operator *.

    EXCEPTIONS = [FooException, BarException]
    
    begin
      a = rand
      if a > 0.5
        raise FooException
      else
        raise BarException
      end
    rescue *EXCEPTIONS
      puts "rescued!"
    end
    

    If you are going to use a constant for the array as above (with EXCEPTIONS), note that you cannot define it within a definition, and also if you define it in some other class, you have to refer to it with its namespace. Actually, it does not have to be a constant.


    Splat Operator

    The splat operator * “unpacks” an array in its position so that

    rescue *EXCEPTIONS
    

    means the same as

    rescue FooException, BarException
    

    You can also use it within an array literal as

    [BazException, *EXCEPTIONS, BangExcepion]
    

    which is the same as

    [BazException, FooException, BarException, BangExcepion]
    

    or in an argument position

    method(BazException, *EXCEPTIONS, BangExcepion)
    

    which means

    method(BazException, FooException, BarException, BangExcepion)
    

    [] expands to vacuity:

    [a, *[], b] # => [a, b]
    

    One difference between ruby 1.8 and ruby 1.9 is with nil.

    [a, *nil, b] # => [a, b]       (ruby 1.9)
    [a, *nil, b] # => [a, nil, b]  (ruby 1.8)
    

    Be careful with objects on which to_a is defined, as to_a will be applied in such cases:

    [a, *{k: :v}, b] # => [a, [:k, :v], b]
    

    With other types of objects, it returns itself.

    [1, *2, 3] # => [1, 2, 3]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some VBA code that needs to talk to a running c# application.
I have some code that needs to work with X509 Certificate information. I have
I have some Javascript code that needs to end with either a true or
I have some classic asp code that needs converting to asp.net. So far I
I have some parallel code (implemented using MPI) that needs to be documented. I'd
I have some code that needs to be able to find the version number
In my winforms application I have some code that needs to be executed after
I have some VB6 code that needs to be migrated to VB.NET, and I
I have some SerialPort code that constantly needs to read data from a serial
I have some code that needs to know how many actual cores are available

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.