0

I read that Ruby doesn't have nested rescues. Is that why this won't work?

begin
    dosomething
rescue => e
    #this is never executed, dosomething raises the error directly
end

def dosomething
    SomeModel.find(-1) #this raises the exception instead of above
end
Natus Drew
  • 1,876
  • 1
  • 21
  • 23

1 Answers1

-1

Good day. You must read ruby doc about exceptions In short: Exception - base class for All exceptions, therefore base code:

begin
  # some code
rescue Exception => e
end

You should always specify which exception you handle If you need proc all - use class Exception

enter image description here

P.S. For you self education - http://rubylearning.com/satishtalim/ruby_exceptions.html

diderevyagin
  • 334
  • 1
  • 7
  • Are you saying that rescue => e won't ever be executed because it doesn't include the kind of exception to rescue whether it's Exception or something more granular? – Natus Drew Apr 26 '15 at 16:53
  • If you don't specify which exception it defaults to StandartError and you probably want to handle StandartError instead of Exception. see http://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby – hamdiakoguz Aug 14 '15 at 15:39