error handling - Ruby rescue doesn't catch a StandardError -


i'm writing tests ruby rails application, , have block of code supposed catch error thrown redis server if ruby cannot connect it. currently, code looks this:

begin     config.before(:all) { resque.redis.select 1 }     config.after(:all) { resque.redis.keys("queue:*").each { |key| resque.redis.del key } } rescue exception     puts "rescued redis error" end 

according stack trace when try run tests, second line of code snippet -- config.before(:all) {...} -- throws redis::cannotconnecterror. after lot of "e.class.superclass.superclass..." commands, determined error inherited standarderror.

after got stuck. tried catching error "rescue redis::cannotconnecterror", "rescue", , "rescue exception", error still thrown. however, tried same things in ruby command prompt, , exception caught every time

could me work out what's happening here? thanks!

the problem blocks passed before , after not being executed @ time they're defined; instead, they're being stored , called later rspec before , after each spec file runs.

you'll want move begin/rescue within blocks instead:

config.before(:all)   begin     resque.redis.select 1   rescue exception     puts "rescued redis error"   end end  # same config.after(:all) 

Comments