gpt4 book ai didi

ruby-on-rails - 如何检查 Rails 3.x 中错误处理的特定救援条款?

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

我有以下代码:

begin
site = RedirectFollower.new(url).resolve
rescue => e
puts e.to_s
return false
end

抛出如下错误:

方案http不接受注册表部分:www.officedepot.com;

方案http不接受注册表部分:ww2.google.com/something;

操作超时 - connect(2)

如何为所有类似方案 http 不接受注册表部分 的错误添加另一个救援?

因为我想做的不仅仅是打印错误并在这种情况下返回 false。

最佳答案

视情况而定。

我看到三个异常描述不一样。异常类型也不同吗?

如果这样你可以这样写你的代码:

begin
site = RedirectFollower.new(url).resolve
rescue ExceptionType1 => e
#do something with exception that throws 'scheme http does not...'
else
#do something with other exceptions
end

如果异常类型相同,那么您仍然只有一个救援 block ,但将根据正则表达式决定要做什么。也许是这样的:

begin
site = RedirectFollower.new(url).resolve
rescue Exception => e
if e.message =~ /the scheme http does not accept registry part/
#do something with it
end
end

这有帮助吗?

关于ruby-on-rails - 如何检查 Rails 3.x 中错误处理的特定救援条款?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8318044/

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