如何通知呼叫者有关失败的原因 [英] How inform the caller about the failure reason
问题描述
如果方法失败,有几个导致失败的原因,呼叫者(用户界面层)可以用来向用户显示适当的消息(这样用户可以解决问题)。
我在想使用适当的消息抛出异常,但失败原因是正常执行路径的一部分(与方法成功相比,几乎有20%的几率发生)。所以例外没有意义。
另一个选项是定义一个枚举,并用它来通知调用者失败原因(使用元组
作为返回类型或添加输出
参数)。我没有看到这样的设计!
通知消息者失败原因的最佳做法是什么?
对于20%的情况,没有任何异常协议的耻辱,除非这是从性能的角度来看非常关键的路径。传入一个阵列,状态出来的作品,但它会看起来很丑陋。
I have a method which is supposed to do something and the return value is a boolean (success/failure).
If the method fails, there are a few reasons causing the failure which the caller (user interface layer) can use to show an appropriate message to the user (this way user can fix the problem).
I was thinking of throwing exceptions with appropriate message but failure reasons are part of normal execution path (almost have 20% chance to occur in compare to method success). So exceptions don't make sense.
Another option is defining an enumeration and used it to inform the caller about the failure reason (using a Tuple
as return type or adding an out
parameter). I didn't have seen this kind of design before!
What's the best practice to inform the caller about the failure reason ?
There's no shame in having an exception protocol for the 20% case, unless this is in a very critical path from a performance point of view. Passing in an array into which the status comes out works, but it's going to look ugly.
这篇关于如何通知呼叫者有关失败的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!