Accessing object in rescue block

F

Florian Odronitz

Hi,

when I rescue an exception, is there a way to access the object where
the exception was raised?

For example:

begin
"something".nonexistent
rescue NoMethodError => e
# here
end

In the rescue block, can I access the String "something"?

Best,
Florian
 
L

Lars Olsson

Hi,

when I rescue an exception, is there a way to access the object where
the exception was raised?

For example:

begin
  "something".nonexistent
rescue NoMethodError => e
  # here
end

In the rescue block, can I access the String "something"?

Best,
Florian

Not by default. According to http://www.ruby-doc.org/ruby-1.9/classes/Exception.html
an exception provides "...information about the exception—its type
(the exception’s class name), an optional descriptive string, and
optional traceback information..."

However the same page also says "...Programs may subclass Exception to
add additional information."

So, if your roll your own exceptions it's possible to add this
functionality.

class MyException < Exception

attr_accessor :source

def initialize(msg = nil, source = nil)
super(msg)
@source = source
end
end

class Test
def enclose(obj)
if obj.kind_of?(String)
"(#{obj})"
else
raise MyException.new('Enclose needs a string!', obj)
end
end
end

begin
t = Test.new
puts t.enclose('foo')
puts t.enclose(5)
rescue MyException => err
puts "Error: #{err.message}"
puts "Source: #{err.source}"
end

Overriding the default Exception class might also be possible (after
all, it's ruby) but changing "default" behavior can be pretty risky as
well.

/lasso
 
R

Robert Klemme

when I rescue an exception, is there a way to access the object where
the exception was raised?

For example:

begin
"something".nonexistent
rescue NoMethodError => e
# here
end

In the rescue block, can I access the String "something"?

There is no way I am aware of which allows for fetching the instance
from the NoMethodError if this is what you want. If you display the
exception you will see a textual description probably derived from #inspect:

irb(main):001:0> begin
irb(main):002:1* "".foo
irb(main):003:1> rescue Exception => e
irb(main):004:1> puts e.display
irb(main):005:1> end
undefined method `foo' for "":String
=> nil
irb(main):006:0>

If that is not sufficient for you, you can reference a variable defined
after "begin":

irb(main):006:0> begin
irb(main):007:1* x = ""
irb(main):008:1> x.foo
irb(main):009:1> rescue Exception => e
irb(main):010:1> p x
irb(main):011:1> end
""
=> ""
irb(main):012:0>

Kind regards

robert
 
F

Florian Odronitz

Thanks for your thoughts, I figured it out.

I was coming from this Article:
http://rbjl.net/26-the-28-bytes-of-ruby-joy

The author suggests to let the method_missing of Nil always return nil
so you could do things like:
some_object.this_is_null.do_something_more
would return nil and not raise on the undefined method
'do_something_more' on nil.
I thought such an approach was to intrusive and it would be better to do
something like:

ignore_nil{some_object.this_is_null.do_something_more}

My current implementation looks like this:

class NoMethodErrorInNil < NoMethodError; end

class NilClass
def method_missing(method_name, *args)
raise NoMethodErrorInNil, "undefined method `#{method_name}' for
nil:NilClass"
end
end

class Object
def ignore_nil(return_value = nil, &block)
begin
yield
rescue NoMethodErrorInNil => e
return return_value
end
end
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top