gpt4 book ai didi

Ruby 异常处理 : can't suppress NoMethodError

转载 作者:数据小太阳 更新时间:2023-10-29 07:45:53 26 4
gpt4 key购买 nike

我只是希望我的方法调用能够抑制它依次调用的方法中可能出现的所有“NoMethodError”异常。

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

但这行不通。 NoMethodError 一直被提升到顶层。

确切的错误是undefined method[]' for nil:NilClass' (NoMethodError)

最佳答案

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

如果你想要一些不那么全局的东西,你可以在一个更窄的类上,甚至是一个特定的实例上这样做:

class << my_object
# and so forth

关于Ruby 异常处理 : can't suppress NoMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/686420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com