Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby exception handling: can't suppress NoMethodError

Tags:

ruby

I simply want my method call to suppress all "NoMethodError" exceptions that might arise in the methods it in turn calls.

def foo
  begin
    bar1
    bar2
  rescue Exception
    return '--'
  end
end

But this doesn't work. NoMethodError keeps being raised to the top level.

The exact error is undefined method[]' for nil:NilClass' (NoMethodError)

like image 318
Aaron Fi Avatar asked Dec 06 '25 05:12

Aaron Fi


1 Answers

class Object
    def method_missing(meth,*args)
        # do whatever you want here
        end
    end

If you want something less global, you can do this on an narrower class, or even a specific instance:

class << my_object
    # and so forth
like image 141
MarkusQ Avatar answered Dec 09 '25 15:12

MarkusQ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!