如何通知呼叫者有关失败的原因 [英] How inform the caller about the failure reason

查看:114
本文介绍了如何通知呼叫者有关失败的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,应该做一些事情,返回值是一个布尔值(成功/失败)。



如果方法失败,有几个导致失败的原因,呼叫者(用户界面层)可以用来向用户显示适当的消息(这样用户可以解决问题)。



我在想使用适当的消息抛出异常,但失败原因是正常执行路径的一部分(与方法成功相比,几乎有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆