我需要Timeout::Error从Redis库中拯救一个凸起但是我遇到了一个问题,抢救那个特定的类似乎不起作用.
begin
Redis.new( { :host => "127.0.0.X" } )
rescue Timeout::Error => ex
end
=> Timeout::Error: Timeout::Error from /Users/me/.rvm/gems/ree-1.8.7-2011.03@gowalla/gems/redis-2.2.0/lib/redis/connection/hiredis.rb:23:in `connect'
Run Code Online (Sandbox Code Playgroud)
当我试图拯救Exception它仍然无法正常工作
begin
Redis.new( { :host => "127.0.0.X" } )
rescue Exception => ex
end
=> Timeout::Error: Timeout::Error from /Users/me/.rvm/gems/ree-1.8.7-2011.03@gowalla/gems/redis-2.2.0/lib/redis/connection/hiredis.rb:23:in `connect'
Run Code Online (Sandbox Code Playgroud)
如果我尝试手动提升异常,我可以拯救它,但不知道为什么我从Redis Gem(2.2.0)中调用它时无法解救它.
begin
raise Timeout::Error
rescue Timeout::Error => ex
puts ex
end
Timeout::Error
=> nil
Run Code Online (Sandbox Code Playgroud)
有任何线索如何解救这个例外?
我正在使用Ruby on Rails 3.2.2,我想通过引发"自定义"错误消息来正确地挽救以下流程:
def rescue_method
# sample_string.class
# => String
# sample_string.inspect
# => "ARubyConstantThatDoesNotExist"
begin
build_constant(sample_string)
rescue
raise("My custom error message: #{build_constant(sample_string)} doesn't exist.")
end
end
def build_constant(sample_string)
"AModule::#{sample_string}".constantize
end
Run Code Online (Sandbox Code Playgroud)
注意:我觉得"强制" constantize在引发的"自定义"消息中也使用该方法以便干掉代码...
当rescue_method执行时,似乎raise("My custom error message")代码永远不会执行,我收到以下错误:
uninitialized constant AModule::ARubyConstantThatDoesNotExist
Run Code Online (Sandbox Code Playgroud)
如何正确显示引发的"自定义"消息(因为在后续引发的"自定义"消息中引发了进一步的错误异常)?你有什么建议?
我偶尔会遇到一些意外错误,例如超时错误,503错误等.有些错误我甚至不知道我可能收到的错误.做以下事情我无法解释所有这些:
rescue Timeout::Error => e
Run Code Online (Sandbox Code Playgroud)
救援也是一个可怕的想法Exception.
我可以使用哪种替代方案?当出现错误时,我希望我的代码能够拯救所有这些代码; 如果没有错误,我需要避免它.我希望能够杀死我的脚本但不会跳过语法错误等.
我正在使用twitter gem 来允许用户从我的应用程序发布推文。
这是我的 tweet.rb 文件
class Tweet < ActiveRecord::Base
belongs_to :user
validates :user_id, :tweet, presence: true
before_create :post_to_twitter
def post_to_twitter
begin
user.twitter.update(tweet)
rescue Twitter::Error => error
// I want to do a redirect_to root_path, notice: "Please fix the error #{error.message}"
// but its against MVC structure to pattern for a model to redirect, since its the task of a controller. How can I achieve this in controller
end
end
end
Run Code Online (Sandbox Code Playgroud)
在 post_to_twitter 方法的救援块中,我想做一个redirect_to root_path, notice: "Please fix …
我正在尝试使用下面的类中的异常,但是当我调用该kivetel方法时,程序总是失败。我认为它只会调用重试部分,而不是满足后置条件。但是它失败,并带有“ y_above_zero”后置违规。
class
KEYWORDS
create
make
feature
y:INTEGER
make
do
end
kivetel
do
ensure
y__above_zero: y > 0
rescue
y := 20
retry
end
end
Run Code Online (Sandbox Code Playgroud) begin
do_something
rescue
Logger.write ...
...error handling...
end
Run Code Online (Sandbox Code Playgroud)
问题是救援中的代码可能引发异常。对于此用例,我想取消它。
那么,如何在救援中包装救援以抑制Exception?
我正在使用Microsoft计算机视觉API.API可以识别面部,并提供有关图像中有多少人的数据,估计的年龄,以及估计的性别.但是,我有一个"do"循环,我无法"拯救".这是下面的代码:
values = json_data['faces'].map do |result|
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误:
C:/Users/KVadher/Desktop/micr1.rb:122:in `block in <main>': undefined method `[]' for nil:NilClass (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
我希望我的代码看起来像这样:
begin
values = json_data['faces'].map do |result|
rescue
end
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,我收到以下错误:
C:/Users/USERNAME/Desktop/micr1.rb:123: syntax error, unexpected keyword_rescue
Run Code Online (Sandbox Code Playgroud)
如果请求不适用,我如何传递我的代码?