ruby - Ruby::Custom Exception 类中的 NoMethodError

标签 ruby ruby-on-rails-3

我有一个自定义异常类:

module ABC
  class XYZ < Exception

  end
end

我尝试在其他类中调用我的异常类::

raise ABC::XYZ "My Msg" if something != onething

我得到以下异常:

NoMethodError: undefined method `XYZ' for ABC:Module

最佳答案

你只是少了一个逗号,该行应该是:

raise ABC::XYZ, "My Msg" if something != onething

目前它被解析为对 XYZ 的方法调用与 "My Msg"作为参数,由于没有 XYZ 而给出错误方法。

关于ruby - Ruby::Custom Exception 类中的 NoMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23230009/

相关文章:

ruby - 测试 Ruby 字符串或符号的字符串相等性(不是对象相等性)的最简洁方法?

ruby-on-rails - Rails 路由范围默认基于 current_user

ruby - 如何用一个参数简化 ruby​​ block ?

mysql - Rails 时间戳 : complex order by updated_at/created_at

ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中

ruby-on-rails - Rails PostgreSQL 数字字段溢出错误

ruby - Rails:ParameterFilter::compiled_filter 尝试复制符号

ruby - RVM——警告! PATH 设置不正确

ruby - 如何修改ruby中的字符串值?

ruby-on-rails - 更新属性而不改变 updated_at 字段