我上的概念真的很困惑:在Ruby中引發異常返回false-何時以及爲什麼?
- 不要使用異常控制流
- 不要返回零/ 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
我強烈推薦http://exceptionalruby.com/ – DGM