How to handle incorrect values in a constructor?

后端 未结 11 1366
野性不改
野性不改 2020-12-17 08:44

Please note that this is asking a question about constructors, not about classes which handle time.

Suppose I have a class like this:



        
11条回答
  •  死守一世寂寞
    2020-12-17 09:23

    Typically you'd have a private/protected constructor and a public static factory method to create the Time object. It is not a good idea to throw an exception from a constructor because it wreaks havoc on inheritance. This way your factory method can throw an exception if needed.

提交回复
热议问题