1

I use 'rubyoverflow' gem in rails:

begin
  puts "=== 1 ==="
  qt = Questions.retrieve_by_tag(tag).questions
  puts "=== 2 ==="
rescue
  puts "=== 3 ==="
end

But sometimes I get the error on the console:

couldn't parse YAML at line 843 column 4

C:/Ruby192/lib/ruby/1.9.1/psych.rb:148:in parse' C:/Ruby192/lib/ruby/1.9.1/psych.rb:148:inparse_stream' ... rubyoverflow (1.0.1) lib/rubyoverflow.rb:86:in request' ... C:/Ruby192/lib/ruby/1.9.1/webrick/server.rb:183:inblock in start_thread'

But the question not about the error in the gem, but about the exception handling. I get in the console only line:

  puts "=== 1 ==="

but not lines

  puts "=== 2 ==="
  puts "=== 3 ==="

why?

How can I restore esecution of my program if the line

  qt = Questions.retrieve_by_tag(tag).questions

fails?

1 Answer 1

2

By default rescue traps StandardError and whatever inherits from it.

From the docs:

By default, rescue only intercepts StandardError and its descendants...

The exception being raised is probably not that, so rescue doesn't handle it.

Usually you can figure out what to use as rescue's parameter from the docs to the method raising it, but, if not, you can use

rescue Exception => e
  print e.to_s
end

to see what the exception is, then replace Exception with that value.

More information is on the internet, but here's a piece of code to print a list of Exceptions.

Sign up to request clarification or add additional context in comments.

1 Comment

That's not it. Notice it is an exception being thrown on another thread: /webrick/server.rb:295:in 'block in start_thread'. I ran into the same problem with rsolr Sunspot. It creates a callback on the model save, and it seems there is no way to catch it! I tried to catch the specific exception Errno::ECONNREFUSED, and also Exception, but it still fails. The stack trace doesn't list any of my code whatsoever. It is all framework code, from Webbrick down to rack down to rsolr.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.