简体   繁体   中英

Winform / usercontrols - exception type to use when a property is null

This question has probably been asked before in some shape of form before, but I haven't seen an answer in the context of winform and usercontrols.

From what I understand usercontrols and winform should have parameterless constructors (so they work properly in the designer). As a result, using properties is a good work around. However, what exception should you throw (if any) if an important property is not set?

Is it a good idea to throw an argument null exception from a property getter? Or should I be checking at the beginning of a methods, and throw the exception from there?

Any help would be most appreciated.

InvalidOperationException seems appropriate. The operation is invalid given an object's current state. However, I have also seen it said that you should not throw framework exceptions, but derive your own. I'm not sure what to believe.

Edit: Based on the example at http://msdn.microsoft.com/en-us/library/xhcbs8fz.aspx I think it would be OK to throw an InvalidOperationException so long as the message is specific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM