.NET Return value vs thrown Exception Design question

后端 未结 4 1079
无人及你
无人及你 2021-01-19 23:44

Let\'s assume we have a method handling operations in a tree hierarchical data structure located in a class handling such a structure.

Let\'s look closer at one of t

4条回答
  •  天涯浪人
    2021-01-20 00:08

    If it is a unexpected runtime error, as here, it's an exception, otherwise it should be a return value. Model your decision on int.Parse (throws) / int.TryParse (return value), the first is for circumstances where you know things must be int (parsing a typed structure for example), the other is for validating user input (typing errors are expected from the user).

    Exception handling is runtime expensive when thrown, it should avoided in a loop.

提交回复
热议问题