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

  • SEARCH
  • Home
  • 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 8162797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:50:59+00:00 2026-06-06T18:50:59+00:00

Is there a way of doing the old on error resume next routine in

  • 0

Is there a way of doing the old “on error resume next” routine in ruby?

I’ve got array of value filled in dynamically from elsewhere (read from MQTT topics to be precise) then I want to do a bunch of numeric calculations on them and publish the results. The values SHOULD be numeric but are possibly missing or non-numeric.

At the moment my code looks something like

values=[]


//values get loaded here 

begin
  Publish('topic1',value[0]*10+value[1])
rescue TypeError,NoMethodError,ZeroDivisionError
end

begin
  Publish('topic2',value[3]/value[4])
rescue TypeError,NoMethodError,ZeroDivisionError
end

//etc etc

If the calculation fails for any reason the program should just skip that step and go on.

It works but surely theres a better way than all those identical begin..rescue blocks? Ruby is about “DRY” after all..

Is there a way of re-writing the above so that a single begin..rescue construct is used while still allowing all calculations to be attempted?

UPDATED

How safe to do something like

def safe_Publish(topic,value)
  return if value.nil?
  Publish(topic,value)
end

and call with
safe_Publish(‘topic2’,(value[3]/value[4] rescue nil))

The main problem is that the above catches ALL exceptions not just the ones I’m expecting which makes me a little nervous.

  • 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-06-06T18:51:00+00:00Added an answer on June 6, 2026 at 6:51 pm

    The on error resume next coding style is really dangerous – as it makes finding new bugs you accidentally introduce to your program very hard to find. Instead, I would just write a different version of publish that doesn’t throw those exceptions:

    def try_publish(topic_name)
      begin
        Publish('topic1',yield)
      rescue TypeError,NoMethodError,ZeroDivisionError
        # are you sure you don't want to do anything here? Even logging the errors
        # somewhere could be useful.
      end
    end
    

    You can then call this with:

    try_publish('topic1') { value[0]*10+value[1] }
    

    If TypeError,NoMethodError or ZeroDivisionError are thrown by the expression, they will be caught and ignored.

    Now your original method won’t require any rescues.


    If you really wanted an on error resume next, you could possibly do it by monkey patching the raise method in Kernel, but that would be a horrible idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to make MySQL cause some kind of error when doing
Is there a way of doing a find_by_x that raises an exception if the
Is there a simple way of doing this I found this online and I
Hi I'm sure there is some way of doing what I want, but maybe
I wonder if there's some way of doing this, or even if it should
Is there a more efficent way of doing this in terms of memory usage
Is there any way to specify a width when doing :vsplit? Also, is there
Is there a way to check what a asp.net process is doing in production
Is there a way to run the changed files through a filter before doing
The old way that I was doing this in iOS 4 was by declaring

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.