ceth
ceth

Reputation: 45325

exception handling

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?

Upvotes: 1

Views: 946

Answers (1)

the Tin Man
the Tin Man

Reputation: 160631

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.

Upvotes: 2

Related Questions