How inform the caller about the failure reason

蹲街弑〆低调 提交于 2019-12-10 22:54:52

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/5054235/how-inform-the-caller-about-the-failure-reason

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!