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

Non-fatal rescue in Rails

I'm trying to run a command that might fail sometimes. When it fails, it throws an exception. What I'd like it to do is just log the error quietly and continue executing the next line below it, rather than aborting and going into the 'rescue' block.…
cjm2671
  • 18,348
  • 31
  • 102
  • 161
0
votes
2 answers

Begin, Rescue with API endpoint

Here is my method: def get_video_duration @time ||= Panda.get("/videos/#{@video.panda_id}/metadata.json")["duration"] format_duration end I need to write this method "better" in wrapping it with a begin, rescue block so that @time could be nil…
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
0
votes
2 answers

Eiffel exception not work

I'm trying to use an exception like in the class below, but the program always fails when I call the kivetel method. I'd think that it'll just call retry part, than it will satisfy the postcondition. But it fails with "y_above_zero" postcond…
Ferenc Dajka
  • 1,052
  • 3
  • 17
  • 45
0
votes
2 answers

Undefined method - record_not_found

class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :null_session rescue_from ActiveRecord::RecordNotFound,…
TheWebs
  • 12,470
  • 30
  • 107
  • 211
0
votes
1 answer

Creating an image of a drive cloned with ddrescue.

We have an old server with disk failures that we've tried to clone into VMSphere. This resulted in an error from what that error came from we couldn't pin point. With ddrescue we cloned the machine to a 2TB external hard drive that we can use to lab…
HenrikM
  • 427
  • 6
  • 19
0
votes
1 answer

Ruby begin rescue end with if-then-else

I am kind of stuck with using begin-rescue-end with if-else-end. Please see the code snippet below. def fn1 unless fn2? puts "Message 1" return end puts "Message 2" end def fn2? begin rescue …
Sunshine
  • 479
  • 10
  • 24
0
votes
2 answers

Variables not recognized within Rescue in Ruby

I have the following code: rescue Timeout::Error, StandardError => e puts "Caught exception: #{e.message}".red log.puts("#{e.backtrace}") email_ids_all.each do |email_delete| call= "/api/v2/emails/#{email_delete}/" …
Luigi
  • 5,443
  • 15
  • 54
  • 108
0
votes
1 answer

using rescue in variable assignment

I have this User.new ( gender: auth.extra.raw_info.gender.capitalize, ...) auth is a hash that looks like this auth => {:extra => { :raw_info => { :gender => ... , .. }, ..} ..} sometimes, for whatever reason, the gender doesn't exist and I want…
Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244
0
votes
1 answer

Do I have to rescue exceptions inside my loop using Ruby?

I am learning more and more about ruby and have written a script that times out on occassion (making several API calls). I know I need to rescue the exception with something like this: rescue Timeout::Error => e # log #{e} for later, maybe end My…
Luigi
  • 5,443
  • 15
  • 54
  • 108
0
votes
1 answer

How to repeatedly handle an exception until proper result?

I have a custom exception that I want raised and rescued for as many times as performing the method causes the error. I know that it will eventually result in a exception free result. Using begin/rescue/end it seems like when the exception is…
Steven Harlow
  • 631
  • 2
  • 11
  • 26
0
votes
1 answer

Catching exceptions while using an external gem

I have written a program that utilizes an external ruby gem. As I am doing a lot of different actions with this, I want to be able to rescue and handle exceptions across the board, instead of implementing it each time I call a method. What is the…
Jericon
  • 4,992
  • 3
  • 20
  • 22
0
votes
1 answer

Exception is only caught with `rescue` at the end of the line but not when using a `begin rescue` block

I have a statement that fails: result = service.load_data() Now the following suppresses the error and I can then check for nil result = service.load_data() rescue nil But when I do the following the initial error is thrown right up to the UI and…
Besi
  • 22,579
  • 24
  • 131
  • 223
0
votes
1 answer

Catching errors with Ruby Twitter gem, caching methods using delayed_job: What am I doing wrong?

What I'm doing I'm using the twitter gem (a Ruby wrapper for the Twitter API) in my app, which is run on Heroku. I use Heroku's Scheduler to periodically run caching tasks that use the twitter gem to, for example, update the list of retweets for a…
JoshDoody
  • 417
  • 1
  • 4
  • 14
0
votes
1 answer

How to rescue from bad url

I have a method in my application that finds a photo from the og:image tag of a link that is submitted. In my create action, I use the method photo_form_url, described below. def photo_from_url(url) if…
umezo
  • 1,519
  • 1
  • 19
  • 33
0
votes
2 answers

Rescue "RecordNotFound" errors in DelayedJob workers

I use DelayedJob to handle certain tasks in the background. For example, in my application a user can "like" a message, and when that happens the poster gets notified. This notification is handled in the background. Occasionally, what can happen is…
pejmanjohn
  • 1,057
  • 3
  • 12
  • 26
1 2 3
13
14