Exception for missing data

后端 未结 7 920
难免孤独
难免孤独 2021-02-04 23:12

I was wondering what kind of exception should one throw for missing data. For example if an xml node doesn\'t contain data. It would be easy to \"throw new Exception(...

7条回答
  •  我在风中等你
    2021-02-05 00:07

    As a rule of thumb you should throw exceptions in Exceptional Circumstances. If the data in question adversely affects the object’s state or behaviour then throw a custom exception. An alternative approach might involve some kind of validator that fires events which your client handles gracefully, for example, report the error to end-user or insert default values.

    I had a similar problem you described in which I had 2 clients (call them A & B) reading and modifying a single xml file. Client A deleted node X then Client B attempted to update node X. Clearly, updating a node that no longer exists is a problem. To solve this problem I took inspiration from SQL Server which reports the number of rows affected by an UPDATE statement. In this particular case I raised the UpdateNode event as normal with a number of rows affected property set to zero.

提交回复
热议问题