Fail vs. raise in Ruby : Should we really believe the style guide?

前端 未结 2 1605
栀梦
栀梦 2020-12-16 09:25

Ruby offers two possibilities to cause an exception programmatically: raise and fail, both being Kernel methods. According to the doc

2条回答
  •  孤街浪徒
    2020-12-16 09:39

    use 'raise' for exceptions to be caught, and 'fail' for serious errors which are not meant to be handled

    This is not what the official style guide or the link you provided say on the matter.

    What is meant here is use raise only in rescue blocks. Aka use fail when you want to say something is failing and use raise when rethrowing an exception.

    As for the "does it matter" part - it is not one of the most hardcore strictly followed rules, but you could make the same argument for any convention. You should follow in that order:

    1. Your project style guide
    2. Your company style guide
    3. The community style guide

    Ideally, the three should be the same.


    Update: As of this PR (December 2015), the convention is to always use raise.

提交回复
热议问题