Joh*_*hir 12 ruby ruby-on-rails exception
制作一个自己的自定义异常时
class ThingExploded < StandardError; end
class ThingIsMissing < StandardError; end
Run Code Online (Sandbox Code Playgroud)
保存这些的好地方在哪里?我正在考虑lib/exceptions.rb ...并且还在考虑是否更适合以某种方式使它们更接近使用它们的代码.
除非您的例外情况如此严重,否则不应将其从中拯救出来,将其分类Exception是不合适的.
例外,如fatal和NoMemoryError有例外的子类,所以如果你有这样的代码rescue Exception来处理ThingExploded和ThingIsMissing,你会抢救各种东西,最好单独留在家中.
最好从它们中继承它们StandardError.