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
1 answer

Rescue NameError just in this class

I've got a Ruby script and I'm doing this module MyModule class MyClass def do_something begin deployer_object = Object.const_get("MyModule").const_get("#{class_name}Deployer").new(@config, @directory).deploy …
pmerino
  • 5,900
  • 11
  • 57
  • 76
0
votes
1 answer

Undefined method `first' for nil:NilClass

Sometimes I get this error, and I just want Rails to rescue/skip the error when it happens rather than stop the program completely. Is there a good way to do this? Below is my code: <% wiki = MediaWiki.new(:domain => 'commons.wikimedia.org') %> <%…
user749798
  • 5,210
  • 10
  • 51
  • 85
0
votes
1 answer

How to add rescue to simple uri parsing method

I have a simple method that uploads an image from a URL. def photo_from_url(url) if !Nokogiri::HTML(open(url)).css("meta[property='og:image']").blank? photo_url =…
umezo
  • 1,519
  • 1
  • 19
  • 33
0
votes
1 answer

Catch-all rescue

In Ruby/Rails, is there a "catch-all" rescue statement that also allows for more specific rescue possibilities? I tried begin # something rescue URI::InvalidURIError # do something rescue SocketError # do something else rescue # do yet…
JJ Beck
  • 5,193
  • 7
  • 32
  • 36
0
votes
2 answers

Inconsistent Timeout::timeout and rescue Timeout::Error behavior

I'm using Timeout::timeout(1) for a process that takes longer than 1 second, though it only occasionally triggers a timeout. When it does, rescue captures it in different ways each time. Here's a sample of my code: require 'timeout' ... begin …
Nick
  • 9,493
  • 8
  • 43
  • 66
0
votes
1 answer

Ignore Twitter Gem Exceptions

how do I rescue and ignore the exceptions thrown by twitter gem? what I want is this begin Twitter.update(@tweet) rescue Twitter::Error // continue action 1 else // continue action 1 end is there a better way of doing this so I don't…
user1256143
  • 11
  • 2
  • 3
0
votes
1 answer

Exception not caught by rescue block

The problem is in relation to the radis-rb gem. The exception is not caught by my rescue block and my app goes down. My code: begin redis = Redis.new puts "WTF?" rescue Exception puts "Exception" end If redis is down, the message WTF? is…
Paul Brit
  • 5,901
  • 4
  • 22
  • 23
-1
votes
3 answers

Ruby Exception - If Statement rescue doesn't handling exception

I everyone, I've some issue to handling exception in ruby. I doesn't understand why my statement doesn't work. Error : Couldn't find User with id=14 I want to redirect to the login page. def login_required begin if session[:user_id] ==…
Fassbender
  • 121
  • 8
-1
votes
1 answer

Precedence of error handlers: rescue_from vs rescue in a method

In my application_controller.rb I have the following line rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized Now, in one method, I would like to handle that specific error differently and I've added this to one method in a…
fydelio
  • 932
  • 1
  • 8
  • 22
-1
votes
2 answers

effectively applying nil check in ruby

I have this code args = x.lead_details.last.leads.last.country_id rescue nil Function(args) I have used rescue keyword to make sure I don't get errors like undefined method `country_id' for nil:NilClass, undefined method `leads' for…
code0079
  • 147
  • 3
  • 13
-1
votes
1 answer

Ruby exception handling in else block

So I have classic ruby exception handling: begin # do work here rescue SafeShutdown => e # prevent loss of data and safely shutdown rescue SystemExit => e # print #{e} and continue else # how can I get #{e} here to get error message …
Jakub Zak
  • 1,212
  • 6
  • 32
  • 53
-2
votes
1 answer

Ruby 2.3.1p112 SyntaxError for keyword_rescue when using begin

I'm writing a program to validate CSV files in Ruby but I seem to not be using rescue correctly. I made sure to include the begin keyword. I'm using ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu] The code is here: def self.validate begin …
Barn on a Hill
  • 359
  • 3
  • 10
-2
votes
2 answers

Ruby - "Do" loop and "rescue"

I'm using the Microsoft computer vision API. The API can recognise faces and gives data on how many people are in an image, what estimated age they are, and what estimated gender. However, I have a "do" loop which I can't "rescue." Here's the code…
semiflex
  • 1,176
  • 3
  • 25
  • 44
-2
votes
1 answer

Undefined local variable or method `e' inside subscriptions controller

I am getting the following error on my Rails 4.2 application. I'm trying to setup subscriptions with Stripe. A subscription belongs to a business and has_one plan. The error I am getting is below and my code follows. Forgive me if this is a beginner…
-2
votes
1 answer

How do I catch ArgumentError invalid base64 in Ruby?

How do I catch this exception? begin data = Base64.strict_decode64(data) # decode data ... rescue ArgumentError => e logger.severe "Could not decrypt data: #{e}, #{data}" Log ArgumentError (invalid base64): …
Chloe
  • 25,162
  • 40
  • 190
  • 357
1 2 3
13
14