gpt4 book ai didi

ruby - 拯救 NameError 而不是 NoMethodError

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

我需要在特殊情况下捕获 NameError。但我不想捕获 NameError 的所有子类。有办法实现吗?

# This shall be catched
begin
String::NotExistend.new
rescue NameError
puts 'Will do something with this error'
end

# This shall not be catched
begin
# Will raise a NoMethodError but I don't want this Error to be catched
String.myattribute = 'value'
rescue NameError
puts 'Should never be called'
end

最佳答案

你也可以用更传统的方式来做

begin
# your code goes here
rescue NoMethodError
raise
rescue NameError
puts 'Will do something with this error'
end

关于ruby - 拯救 NameError 而不是 NoMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38931178/

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