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
133
votes
5 answers

How does one use rescue in Ruby without the begin and end block

I know of the standard technique of having a begin rescue end How does one just use the rescue block on its own? How does it work, and how does it know which code is being monitored?
Sid
  • 6,134
  • 9
  • 34
  • 57
123
votes
3 answers

Passing multiple error classes to ruby's rescue clause in a DRY fashion

I have some code that needs to rescue multiple types of exceptions in ruby: begin a = rand if a > 0.5 raise FooException else raise BarException end rescue FooException, BarException puts "rescued!" end What I'd like to do is…
apb
  • 3,270
  • 3
  • 29
  • 23
63
votes
8 answers

How can I password-protect my /sidekiq route (i.e. require authentication for the Sidekiq::Web tool)?

I am using sidekiq in my rails application. By Default, Sidekiq can be accessed by anybody by appending "/sidekiq" after the url. I want to password protect / authenticate only the sidekiq part. How can i do that?
sagar junnarkar
  • 1,240
  • 2
  • 10
  • 18
41
votes
2 answers

How to rescue an eval in Ruby?

I'm trying to figure out how to rescue syntax errors that come up when eval()ing code in Ruby 1.8.6. I would expect the following Ruby code: #!/usr/bin/ruby good_str = "(1+1)" bad_str = "(1+1" # syntax error: missing closing paren begin …
Brent Chapman
  • 2,519
  • 3
  • 20
  • 10
25
votes
2 answers

Begin Rescue not catching error

I'm using some ruby code wrapped in a begin - rescue block but somehow it manages to still crash. the block of code looks like this: # Retrieve messages from server def get_messages @connection.select('INBOX') …
Schneems
  • 14,918
  • 9
  • 57
  • 84
23
votes
2 answers

Ruby Oneline Rescue

I recently learned that you can use rescue on a line of code in case something goes wrong on that line (see http://www.rubyinside.com/21-ruby-tricks-902.html Tip #21). I have some code that used to look like this: if obj['key'] && obj['key']['key2']…
Jimmy Z
  • 723
  • 1
  • 5
  • 18
21
votes
5 answers

How to deal with not knowing what exceptions can be raised by a library method in Ruby?

This is somewhat of a broad question, but it is one that I continue to come across when programming in Ruby. I am from a largely C and Java background, where when I use a library function or method, I look at the documentation and see what it…
user85509
  • 36,612
  • 7
  • 33
  • 26
20
votes
1 answer

Rescue_from for javascript requests

In my Rails 2.3.8 application I had a rescue_from code for exceptions, which are thrown during javascript actions: rescue_from ::Exception, :with => :show_js_errors ... def show_js_errors exception if request.format == :js flash[:error] =…
neogrande
  • 209
  • 1
  • 5
16
votes
4 answers

General rescue throughout controller when id not found - RoR

I have stumbled upon a situation where my application looks for an id that does not exist in the database. An exception is thrown. Of course, this is a pretty standard situation for any web developer. Thanks to this answer I know that using rescue…
Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115
15
votes
1 answer

Is nested begin/rescue/ensure valid?

This seems okay to me and I cannot find any documentation that says otherwise, but I'd like it verified. I have a piece of code that could fail, for whatever reason, an ensure after it to protect it if it does fail, then the need to execute some…
Richard_G
  • 4,700
  • 3
  • 42
  • 78
13
votes
3 answers

work with rescue in Rails

I am working with the following piece; def index @user = User.find(params[:id]) rescue flash[:notice] = "ERROR" redirect_to(:action => 'index') else flash[:notice] = "OK" redirect_to(:action => 'index') end Now I either case…
Adnan
  • 25,882
  • 18
  • 81
  • 110
12
votes
1 answer

Elixir: Correct way of printing __STACKTRACE__

I know we can get the full stacktrace using __STACKTRACE__ in a catch/rescue block in elixir, but what's the correct way of printing it? In my case, I rescue from an error but I still want to log it to console. This is what I'm doing right now: def…
Sheharyar
  • 73,588
  • 21
  • 168
  • 215
12
votes
2 answers

How to Rescue from ActionDispatch::ParamsParser::ParseError in Rails 4

Rails 4 adds an exception ActionDispatch::ParamsParser::ParseError exception but since its in the middleware stack it appears it can't be rescued in the normal controller environment. In a json API application I want respond with a standard error…
Kip
  • 684
  • 5
  • 8
10
votes
2 answers

Ruby does not 'ensure' when I 'retry' in 'rescue'

Consider this begin-rescue-ensure block: attempts=0 begin make_service_call() rescue Exception retry unless attempts>2 exit -1 ensure attemps += 1 end If you run that code as it is, it raises an exception because there is no function called…
harithski
  • 666
  • 1
  • 6
  • 18
10
votes
3 answers

Ruby Timeout::timeout doesn't fire Exception and doesn't return what documented

I have this piece of code: begin complete_results = Timeout.timeout(4) do results = platform.search(artist, album_name) end rescue Timeout::Error puts 'Print me something please' end I then launch the method containing this code,…
Pasta
  • 1,750
  • 2
  • 14
  • 25
1
2 3
13 14