Is it a good practice to throw an exception on Validate() methods or better to return bool value?

后端 未结 8 1925
攒了一身酷
攒了一身酷 2020-12-28 14:24

Is it recommended or not to throw exceptions from Validation methods like:

ValidateDates();
ValidateCargoDetails();

Apart from this : Is t

8条回答
  •  清酒与你
    2020-12-28 14:48

    Alot depends on how exceptional validation failures are are how critical it is to be correct.

    If your validation failures are rare and severe or fatal when they occur, I would use Exceptions or even AssertionErrors. Most parsers use Exceptions and these indicate it is not possible to continue processing.

    If your validation failure are expected as under normal operations and do not indicate you cannot continue processing, I would suggest using a visitor pattern or return a List of issues (which can be empty)

提交回复
热议问题