I have the following code:
require 'open-uri'
class CustomException < StandardError;end
def file
f = Kernel.open('http://i.dbastatic.dk/images/2/68/500656768_20012012182407_0401_2.jpg')
return f
rescue => e
raise CustomException.new(e.message)
end
Now if I do the following:
begin
file.body
rescue CustomException
puts "rescued it!"
end
I get:
NoMethodError: undefined method `body' for nil:NilClass
Instead of the the CustomException with the 404 error message from open uri. The strange thing is, that if I instead do:
begin
f = file
f.body
rescue CustomException
puts "rescued it!"
end
Then it works, I get the CustomException, that I can catch, before it's trying to do the .body. I don't get why? And how can I change the file-method to do as I expect?