Ruby offers two possibilities to cause an exception programmatically: raise
and fail
, both being Kernel
methods. According to the documents, they are absolutely equivalent.
Out of a habit, I used only raise
so far. Now I found several recommendations (for example here), to use raise
for exceptions to be caught, and fail
for serious errors which are not meant to be handled.
But does it really make sense? When you are writing a class or module, and cause a problem deep inside, which you signal by fail
, your programming colleagues who are reviewing the code, might happily understand your intentions, but the person who is using my code will most likely not look at my code and has no way of knowing, whether the exception was caused by a raise
or by fail
. Hence, my careful usage of raise
or fail
can't have any influence on his decision, whether she should or should not handle it.
Could someone see flaws in my arguments? Or are there other criteria, which might me want to use fail
instead of raise
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…