【发布时间】:2014-05-22 19:01:41
【问题描述】:
我真的对以下概念感到困惑:
- 不要将异常用作控制流
- 不要将 nil/false 作为异常返回
假设我有以下实例方法:
class Logo
# This method has some logic to create an image using Rmagick
def process
begin
@logo_image = RmagickHelper.new(self.src)
rescue Magick::ImageMagickError
raise Exceptions::LogoUnprocessable, "ImageMagick can't process the URL"
end
end
end
所以在更一般的方法中,我有以下内容:
def build_all_images
begin
@logo.process
rescue Exceptions::LogoUnprocessable
@logo.status = 'unprocessable'
return false #This terminates the method so no more stuff is processed, because logo could not be processed.
end
#....
end
我的问题是:
这样做是否正确:
raise Exceptions::LogoUnprocessable, "ImageMagick can't process the URL"
或者我应该刚刚完成
return false
【问题讨论】:
-
我强烈推荐exceptionalruby.com
标签: ruby-on-rails ruby exception-handling