【问题标题】:Ruby: rescue an OAuthException exceptionRuby:拯救 OAuthException 异常
【发布时间】:2013-06-28 01:29:02
【问题描述】:

我偶尔会收到 OAuthException 并试图通过以下方式捕获它:

rescue OAuthException => exception
# exception handling code here

但是我得到:

rescue in <main>': uninitialized constant OAuthException (NameError)

知道我错过了什么吗?

==== 更新

这是我目前解决的方法。我必须做一个 message.match() 的事实似乎有点 hacky。

rescue GemModule::GemSubmodule::APIError => exception
    if exception.message.match("OAuthException") 

有什么改进吗?

【问题讨论】:

  • 我没有收到日志条目。它只是退出给出begin 语句的行号和上面的rescue in &lt;main&gt; 消息。
  • 是的,有一个父类传递这个异常。
  • 如果引发APIError,是否需要根据错误信息做具体的事情?

标签: ruby exception


【解决方案1】:
begin
  raise OAuthException, 'hello'
rescue OAuthException => e
  puts e
end

--output:--
1.rb:3:in `rescue in <main>': uninitialized constant OAuthException (NameError)
    from 1.rb:1:in `<main>'

.

class OAuthException < Exception
end

begin
  raise OAuthException, 'hello'
rescue OAuthException => e
  puts e
end

--output:--
hello

错误消息告诉您,ruby 中没有 OAuthException 之类的东西。

【讨论】:

  • 是的,不是在 Ruby 中。异常由 Ruby gem 中的类提供。
  • 然后在你的文件中需要 gem。
猜你喜欢
  • 2011-06-15
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
  • 2018-11-27
  • 2013-06-12
  • 2010-12-15
  • 2010-10-07
  • 1970-01-01
相关资源
最近更新 更多