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
9
votes
2 answers

Does Ruby's $! hold value only in rescue block?

begin raise 'foo' rescue puts $!.inspect # => # ensure puts $!.inspect # => nil end puts $!.inspect # => nil Googled around but didn't find a clear answer. Just want to confirm the life-time(?) of $!, does it hold value…
huocp
  • 3,898
  • 1
  • 17
  • 29
9
votes
2 answers

What is the clearest way to open a file and do "rescue" when it cannot be opened in Ruby

I am now dealing with this issue by using the following code begin File.open(filename, 'r') rescue print "failed to open #{filename}\n" exit end Is there any way to do it more easily like Perl 'open (IN, $filename) || die "failed to open…
user3477465
  • 183
  • 2
  • 2
  • 6
9
votes
2 answers

Ruby ignores rescue ArgumentError

When I run the following, rescue seems to be ignored for ArgumentError. The ArgumentError error message from Ruby appears on the console, but my puts message does not. I tried rescue with TypeError and ZeroDivisionError, and it worked. def divide(a,…
Kao Nashi
  • 265
  • 2
  • 9
8
votes
1 answer

Ruby rescue and retry specific code block

I have the following code in my script... begin #Loop to create 1000 emails... #Loop to send 1000 emails... rescue Timeout::Error => e retry_attempts += 1 if retry_attempts < 10 retry else puts "Timeout error,…
Luigi
  • 5,443
  • 15
  • 54
  • 108
7
votes
2 answers

rails 3.1: how can app handle different 'reasons' for ActiveRecord::RecordInvalid (for example, duplicate vs validation error)

In my app, I sometimes create a User on the fly, and a user's email must be a valid format, and be unique. I would like to redirect to different places depending on WHICH validation caused the error to be raised: invalid format vs duplicate. In my…
jpw
  • 18,697
  • 25
  • 111
  • 187
7
votes
4 answers

When creating an object in Ruby on Rails, which method of saving do you prefer, and why?

When writing the "create" method for an object in a Ruby on Rails app, I have used two methods. I would like to use one method for the sake of cleaner and more consistent code. I will list the two methods below. Does anyone know if one is better…
Tony
  • 18,776
  • 31
  • 129
  • 193
7
votes
2 answers

Using transaction in Ruby On Rails controller method

I love Ruby On Rails and every day I am learning and improving my skills. Currently I am working on an application that is used by several clients and I want to refactor my code so I can feel confident about the quality of my code. I am struggling…
MDekker
  • 443
  • 1
  • 4
  • 19
7
votes
2 answers

Rescue all errors of a specific type inside a module

I have a module in which I am performing all of my encryption/decryption tasks for a project. I would like to catch any OpenSSL::Cipher::CipherError exceptions that occur in this module so that I can handle them. Is it possible to do something like…
Matt
  • 13,948
  • 6
  • 44
  • 68
6
votes
1 answer

Ruby c extensions: How can I catch all exceptions, including things that aren't StandardErrors?

In ruby, begin # ... rescue # ... end won't catch exceptions that aren't subclasses of StandardError. In C, rb_rescue(x, Qnil, y, Qnil); VALUE x(void) { /* ... */ return Qnil; } VALUE y(void) { /* ... */ return Qnil; } will do the same thing.…
Adrian
  • 14,931
  • 9
  • 45
  • 70
6
votes
4 answers

Ruby rescue exception during multiple methods

I have built a simple banking application, which is able to perform the usual operations; Deposit, Withdraw etc. My controller methods perform these operations and rescue exceptions that are raised by the account or other entities. Here are some of…
Phil Brockwell
  • 456
  • 5
  • 22
5
votes
5 answers

How do I disable rescue handlers in Ruby on Rails apps when I'm running functional tests?

I have a number of controllers in my Ruby on Rails apps with a rescue handler at the end of the action that basically catches any unhandled errors and returns some kind of "user friendly" error. However, when I'm doing rake test I'd like to have…
Simon Woodside
  • 7,175
  • 5
  • 50
  • 66
5
votes
1 answer

SSLv3 read server certificate B: certificate verify failed (Twitter::Error)

I have received this error message: twitter/rest/client.rb:96:in 'rescue in request' ssl_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (Twitter::Error) My code is: require 'twitter' client =…
ssseee
  • 51
  • 1
  • 3
5
votes
2 answers

How to write down the rspec to test rescue block.?

I have method like this def className def method_name some code rescue some code and error message end end So, How to write down the rspec to test rescue block..?
Dheer
  • 773
  • 1
  • 6
  • 16
5
votes
1 answer

Using single line conditional with require/rescue

I want to avoid an error, if a require is not successfull. I can do it with: begin require 'unexisting_script' rescue LoadError end I tried to do the same with a one-line condition: require 'unexisting_script' rescue LoadError and get the error…
knut
  • 27,320
  • 6
  • 84
  • 112
5
votes
2 answers

What does the "=>" in "rescue Exception => e" do?

Given the example: def method_of_doom my_string = "I sense impending doom." my_string.ah_ha_i_called_a_nonexistent_method rescue NoMethodError => e: puts "PROBLEM: " + e.to_s rescue Exception: puts "Uhh...there's a problem with that there…
Jeremy Iglehart
  • 4,281
  • 5
  • 25
  • 38
1
2
3
13 14