今天看到了Rails Best Practice
中的这么一条建议,Don't rescue Exception:
Don't rescue Exception, rescue StandardError Explicitly rescuing Exception will rescue even not normally recoverable errors such as SyntaxError, LoadError, and Interrupt.
然而我自己在 demo 项目中测试了一下:
def index
a )= 2 # Syntax error here
@books = Book.all
rescue Exception => e
logger.info "#{e}"
end
运行项目时却正常报错了,并没有被 rescue:
SyntaxError (/app/controllers/books_controller.rb:7: syntax error, unexpected ')', expecting keyword_end
a )= 2
^):
我的 rails 版本是 4.2.5,很好奇是否这条规则已经失效了。