Questions tagged [rescue]

The Ruby keyword for catching exceptions.

rescue is used in Ruby to catch exceptions that have been thrown. The general exception handling structure is:

begin
  #...
rescue E1 => e
  #... handle E1
rescue E2 => e
  #... handle E2
else
  #... No exceptions
ensure
  # ... Always executed.
end

You can leave out the begin inside a method:

def m
  #...
rescue
  #...
end

Or use it as a statement modifier

x = blah_blah rescue 11
210 questions
0
votes
2 answers

In Ruby, is it ok to have a double rescue without an end?

I have some code that passes tests in rspec. It appears I don't need end with a double rescue in Ruby, or even begin for that matter. Essentially, I return 5 when a Geocoder error is encountered and 6 if a CustomError is encounted and 7…
Nona
  • 5,302
  • 7
  • 41
  • 79
0
votes
1 answer

Why is this raise not rescued, and what will correct it so that it is?

I am trying to create my own StandardError exception, but I cannot seem to fire rescue_from with the raise. The error is raised but never rescued. I built a simple application to try it, as follows: class ApplicationController <…
Richard_G
  • 4,700
  • 3
  • 42
  • 78
0
votes
1 answer

"Read-only file system" under centos CD rescue mode

I broke centos6.6 by changing libc, now I'm trying revert libc under CD rescue mode, failed as "read-only file system" Am I on the right way or how should fix my system? Thanks
JF.Zheng
  • 1
  • 1
0
votes
1 answer

rescue not catch error 'Selenium::WebDriver::Error'

When my code runs into select_to_city(to), I guess it will break by Selenium::WebDriver::Error But it didn't stop be the rescue why ? class Tiger123 < ClassTemplate def form_action(from, to, flight_date) begin select_to_city(to) …
user3675188
  • 7,271
  • 11
  • 40
  • 76
0
votes
1 answer

Using Ruby, can you use more than one rescue in a begin loop?

Is it possible to add more than one rescue in a begin loop and/or a function and still have a next as well for each? For example: begin twitter_function rescue Twitter::Error::RateLimit => error next rescue Twitter::Error::Unauthorized => error …
marriedjane875
  • 653
  • 2
  • 10
  • 24
0
votes
2 answers

rescue Nokogiri error

I've a simple script that looks at Twitter username and gets me the location. But some of the username doesn't exist and I get error: /usr/lib/ruby/1.8/open-uri.rb:277:in `open_http': 404 Not Found (OpenURI::HTTPError) I've tried to rescue it, but…
sent-hil
  • 18,635
  • 16
  • 56
  • 74
0
votes
1 answer

Transaction unable to Rollback beyond certain Model

I have to process a very long form with multiple models. def registerCandidate action_redirect = "" id = "" ActiveRecord::Base.transaction do begin @entity = Entity.new( name: params[:entity][:name], description:…
james
  • 515
  • 4
  • 14
0
votes
1 answer

In Ruby, can you create a rescue for twitter for when there is an error it will continue the loop?

I'm trying to create a rescue that if and when there is an Twitter::Error::NotFound error (such as does not exist) it will just keep going through the loop. Please help, thanks. Below is the code, begin File.open("user_ids.txt") do |file| …
marriedjane875
  • 653
  • 2
  • 10
  • 24
0
votes
1 answer

Rescue player from going wrong direction in dungeon game

I'm trying to use rescue to save my dungeon game from an error if the player types in a direction that isn't available in the game and will instead repeat their location back to them again and ask where to go. Here's the relevant code: def…
0
votes
1 answer

How regroup all rescues of begin block and switch type of error later?

for this moment I have a block like begin yield rescue MyError => e call_specific_method call_method_foo render json: { error: e.to_s } rescue ActiveRecord::RecordInvalid => e call_specific_method call_method_foo …
Matrix
  • 3,458
  • 6
  • 40
  • 76
0
votes
1 answer

Rails 4, undefined method `rescue_action_in_public'

So I'm trying to do an alias_method on :rescue_action_in_public within a module, included in my controller, when an exception is raised, but I keep getting "undefined method `rescue_action_in_public' from FooController". This is my module: module…
Majoren
  • 983
  • 5
  • 16
  • 36
0
votes
0 answers

Display messages in test/unit result

I want to display the message after test is passed. Where i need to print the message so that i can get in result of test. require 'test/unit' class MyTest < Test::Unit::TestCase def sample begin count = 0 5.times do …
Galet
  • 5,853
  • 21
  • 82
  • 148
0
votes
3 answers

Ruby puts Displaying nil in rescue

I am brand new to Ruby and am coming from a Python background. To help learn the language, I'm porting an existing Python script so I can do a side by side comparison. Thus far, I have a small bit of code and am confused as to why 'nil' prints to…
user164266
  • 65
  • 4
0
votes
1 answer

rescue that calls a method with a rescue

This only prints rescue 1, is there a way to print both rescue 1 and rescue 2? def mimiti raise 'hi there!' rescue puts 'rescue 1' end begin mimiti rescue puts 'rescue 2' end
Arnold Roa
  • 7,335
  • 5
  • 50
  • 69
0
votes
3 answers

Rails rescue/continue loop after each block NoMethodError

I'm retrieving some API information in the following fashion fetch_api.each do |api| save_api = Record.new(name: api.name, height: api.height) save_api.save! end Most records get saved, no problem. But it seems some are missing height and some…
Kasperi
  • 853
  • 7
  • 17