How to indicate that a method was unsuccessful

前端 未结 16 1516
走了就别回头了
走了就别回头了 2020-12-31 08:03

I have several similar methods, say eg. CalculatePoint(...) and CalculateListOfPoints(...). Occasionally, they may not succeed, and need to indicate this to the caller. For

16条回答
  •  时光取名叫无心
    2020-12-31 08:30

    Another alternative is to throw an exception. However, you generally only want to throw exceptions in "exceptional cases".

    If the failure cases are common (and not exceptional), then you've already listed out your two options. EDIT: There may be a convention in your project as how to handle such non-exceptional cases (whether one should return success or the object). If there is no existing convention, then I agree with lucasbfr and suggest you return success (which agrees with how TryParse(...) is designed).

提交回复
热议问题