Timeout::Error isn't rescuing in Ruby

I am still new with Ruby and for the first time I'm trying to use Timeout for some HTTP functions but clearly I'm missing the mark somewhere. My code is below but it's not working. Instead, it raises the following exception: C:/Ruby193/lib/ruby/1.9.1/net/http.rb:762:in `initialize': execution expired (Timeout::Error) Which doesn't make a lot of sense to me since the part of th

Timeout :: Error在Ruby中无法挽救

我对Ruby仍然很陌生,并且第一次尝试将Timeout用于某些HTTP函数,但显然我在某处丢失了标记。 我的代码在下面,但它不起作用。 相反,它会引发以下例外情况: C:/Ruby193/lib/ruby/1.9.1/net/http.rb:762:in `initialize': execution expired (Timeout::Error) 这对我来说没什么意义,因为它的超时代码的一部分被封装在开始/救援/结束块中,特别是拯救Timeout :: Error。 我做错了什么,或者Ruby中不支持的东西? retri

How to handle 404 not found errors in Nokogiri

I am using Nokogiri to scrape web pages. Few urls need to be guessed and returns 404 not found error when they don't exist. Is there a way to capture this exception? http://yoursite/page/38475 #=> page number 38475 doesn't exist I tried the following which didn't work. url = "http://yoursite/page/38475" doc = Nokogiri::HTML(open(url)) do begin rescue Exception => e

如何处理404在Nokogiri中找不到错误

我正在使用Nokogiri来抓取网页。 很少有urls需要被猜测,并且当它们不存在时返回404没有发现错误。 有没有办法捕捉这个异常? http://yoursite/page/38475 #=> page number 38475 doesn't exist 我尝试了以下不起作用的内容。 url = "http://yoursite/page/38475" doc = Nokogiri::HTML(open(url)) do begin rescue Exception => e puts "Try again later" end end 它不起作用,因为你没有拯救部分代

Automatically Logging Exceptions in Ruby

Is there a library or easy way to catch exceptions thrown in a Ruby program and log it to a file? I've looked over log4r and logger, but the docs on both don't provide any examples on how I would do this. I run this program remotely and lose handles to stdout and stderr, if that information helps at all. What would you recommend? If you want to take a walk on the wild side, try this

在Ruby中自动记录异常

有没有一种库或简单的方法来捕获Ruby程序中抛出的异常并将其记录到文件中? 我已经看过log4r和logger,但两者上的文档都没有提供任何有关如何执行此操作的示例。 我远程运行该程序,并丢失stdout和stderr的句柄,如果这些信息有帮助的话。 你会推荐什么? 如果你想在狂野的一边散步,试试这个: class Exception alias real_init initialize def initialize(*args) real_init *args # log the error (self) or

How to signal "not implemented yet"?

In the initial drafting of a new gem I need to leave some method implementations empty ( to be implemented in the next ) Therefore, I would like to signal a "not implemented yet" exception I'm wondering if there is a best practice or standard conventions specific to the Ruby language to code this kind of placeholder / exception. ie: something like: UnsupportedOperationExcept

如何发信号“尚未实施”?

在最初起草一个新的创业板时,我需要留下一些方法实现为空(将在下一个实施) 因此,我想表示一个“尚未实施”的例外情况 我想知道是否有特定于Ruby语言的最佳实践或标准约定来编写这种占位符/例外。 即:类似于: Java中的UnsupportedOperationException .Net框架中的NotImplementedException(C#) 你应该提出NotImplementedError raise NotImplementedError 红宝石DOC 你可以使用todonotes-gem 有一些例子的

Ruby rescue all exceptions except MyException

在Ruby中,是否有可能拯救除指定之外的所有异常? Not as such no. But you can rescue all exceptions and reraise the exception if it is a MyException. begin rescue MyException raise #to reraise the same exception without changing it rescue Exception => e #do something with e end Without knowing more about your problem, I'd suggest Ken Bloom's answer. However, I'd like to know

Ruby除了MyException之外拯救所有异常

在Ruby中,是否有可能拯救除指定之外的所有异常? 不是这样的。 但是,如果它是MyException,则可以挽救所有异常并重新评估异常。 begin rescue MyException raise #to reraise the same exception without changing it rescue Exception => e #do something with e end 不知道更多关于你的问题,我会建议肯布鲁姆的答案。 但是,我想更多地了解您为什么要这么做。 你是否担心一个非常严重的例外情况,而不是想要

How to display error type in ruby?

in the following code begin raise StandardError, 'message' #some code that raises a lot of exception rescue StandardError #handle error rescue OtherError #handle error rescue YetAnotherError #handle error end I want to print a warning stating the type and the message of the error without adding print statement to each of the rescue clauses, like begin raise StandardError, 'message' #som

如何在ruby中显示错误类型?

在下面的代码中 begin raise StandardError, 'message' #some code that raises a lot of exception rescue StandardError #handle error rescue OtherError #handle error rescue YetAnotherError #handle error end 我想打印一个警告,说明错误的类型和消息,而不用为每个rescue子句添加print语句,比如 begin raise StandardError, 'message' #some code that raises a lot of exception rescue StandardError #han

Which is the shortest way to silently ignore a Ruby exception

I'm looking for something like this: raise Exception rescue nil But the shortest way I've found is this: begin raise Exception rescue Exception end def ignore_exception begin yield rescue Exception end end 现在写你的代码ignore_exception { puts "Ignoring Exception"; raise Exception; puts "This is Ignored" } This is provided by ActiveSupport: suppress(Exception) do

这是最简单的方式来默默地忽略Ruby异常

我正在寻找这样的东西: raise Exception rescue nil 但我发现的最短路线是这样的: begin raise Exception rescue Exception end def ignore_exception begin yield rescue Exception end end 现在写你的代码ignore_exception { puts "Ignoring Exception"; raise Exception; puts "This is Ignored" } 这由ActiveSupport提供: suppress(Exception) do # dangerous code here end http://api.rubyo

How to rescue an eval in Ruby?

I'm trying to figure out how to rescue syntax errors that come up when eval() ing code in Ruby 1.8.6. I would expect the following Ruby code: #!/usr/bin/ruby good_str = "(1+1)" bad_str = "(1+1" # syntax error: missing closing paren begin puts eval(good_str) puts eval(bad_str) rescue => exc puts "RESCUED!" end to produce the following result when run: 2 RESCUED! Inste

如何在Ruby中拯救eval?

我试图找出如何解决在Ruby 1.8.6中使用eval()代码时出现的语法错误。 我期望以下的Ruby代码: #!/usr/bin/ruby good_str = "(1+1)" bad_str = "(1+1" # syntax error: missing closing paren begin puts eval(good_str) puts eval(bad_str) rescue => exc puts "RESCUED!" end 运行时产生以下结果: 2 RESCUED! 相反,我得到的是: 2 eval_rescue.rb:8: (eval):1: compile error (SyntaxError) (eval)

How do I add information to an exception message in Ruby?

How do I add information to an exception message without changing its class in ruby? The approach I'm currently using is strings.each_with_index do |string, i| begin do_risky_operation(string) rescue raise $!.class, "Problem with string number #{i}: #{$!}" end end Ideally, I would also like to preserve the backtrace. Is there a better way? To reraise the exception and mo

如何将信息添加到Ruby中的异常消息?

如何将信息添加到异常消息中,而无需在ruby中更改其类? 我目前使用的方法是 strings.each_with_index do |string, i| begin do_risky_operation(string) rescue raise $!.class, "Problem with string number #{i}: #{$!}" end end 理想情况下,我也想保留回溯。 有没有更好的办法? 要重新生成异常并修改消息,同时保留异常类及其回溯,只需执行以下操作: strings.each_with_index do |string, i| be

Ruby custom error classes: inheritance of the message attribute

I can't seem to find much information about custom exception classes. What I do know You can declare your custom error class and let it inherit from StandardError , so it can be rescue d: class MyCustomError < StandardError end This allows you to raise it using: raise MyCustomError, "A message" and later, get that message when rescuing rescue MyCustomError => e puts e.message

Ruby自定义错误类:继承消息属性

我似乎无法找到关于自定义异常类的很多信息。 我所知道的 你可以声明你的自定义错误类并让它继承自StandardError ,所以它可以被rescue d: class MyCustomError < StandardError end 这使您可以使用以下方法进行提升: raise MyCustomError, "A message" 之后,在救援时收到这条消息 rescue MyCustomError => e puts e.message # => "A message" 我不知道的 我想给我的例外一些自定义字段,但我想继承父