0

I have a statement that fails:

result = service.load_data()

Now the following suppresses the error and I can then check for nil

result = service.load_data() rescue nil

But when I do the following the initial error is thrown right up to the UI and I don't get the details of the exception.

begin
   result = service.load_data()
rescue => details         
   logger.fatal "Failed to load the data: #{details}"
end

I am sure there is a silly detail I must be missing but I can't seem to spot the problem here. So why isn't the rescue block invoked?

Update: The error I got was this:

getaddrinfo: nodename nor servname provided, or not known
Besi
  • 22,579
  • 24
  • 131
  • 223
  • What kind of error happens? – Sergio Tulentsev Apr 24 '13 at 17:38
  • @SergioTulentsev `getaddrinfo: nodename nor servname provided, or not known` (I updated my answer) – Besi Apr 24 '13 at 17:39
  • 1
    Check the spelling of 'rescue' (your post has 'resuce'). – Bob Jarvis - Слава Україні Apr 24 '13 at 17:45
  • 1
    You still haven't provided an exception class, but for anyone trying to help, it's probably `SocketError` – Lee Jarvis Apr 24 '13 at 17:45
  • 3
    Also, using `rescue` with no class will default to `StandardError`, if your exception class does not inherit from that, it will fail. You might want `rescue Exception` or specify classes more specifically. That said, if it /is/ SocketError, that inherits from StandardError so your code should work as it is – Lee Jarvis Apr 24 '13 at 17:48
  • @BobJarvis good catch. I corrected it. I did not have this typo in the code though. – Besi Apr 24 '13 at 17:57
  • Ok Priti's answer did the trick. I don't know why I am being downvoted. I do find it a bit peculiar that `rescue` at the end of a line cateches the Exception in this case but when used as a block it doesn't unless you provide the type of the exception. – Besi Apr 24 '13 at 20:31

1 Answers1

2
begin
   result = service.load_data()
rescue AnExceptionKlass => details    # here the name is SocketError    
   logger.fatal "Failed to load the data: #{details}"
end

use the above.

tried to replicate the error here as below:

require 'net/http'
Net::HTTP.start('http://www.google.com') do |http|
response = http.get('/')
puts response
end
#=> getaddrinfo: No such host is known.  (SocketError)

Fixed it as below:

require 'net/http'

begin

  htt = Net::HTTP.start('http://www.google.com')
  response = htt.get('/')
  puts response

rescue SocketError => details    # or the Exception class name may be SocketError    
   p "Failed to load the data: #{details}"
end

#=> "Failed to load the data: getaddrinfo: No such host is known. "
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • NEVER `rescue Exception => e` Don't even suggest it as an "example". https://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby – nitsujri Oct 22 '15 at 03:19
  • @nitsujri Did you read answer completely ? Did you read the comments I gave. come on!! – Arup Rakshit Oct 22 '15 at 08:14