开发者

How inform the caller about the failure reason

开发者 https://www.devze.com 2023-02-11 23:20 出处:网络
I have a method which is supposed to do something and the return value is a boolean (success/failure).

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 exception开发者_StackOverflow社区s 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号