开发者

Which Exception class to choose

Suppose you want to throw Exception like this:

'Project with the provided ID cannot be assigned.'

and you don't want to write your custom Exception class. What predefined Exception class would you use for this? (I'开发者_开发问答m talking about all classes inheriting from Exception)


Why can't the project be assigned? Is it because the ID provided (presumably as an argument) is inappropriate? If so, it should be an ArgumentException. If it's because the project's state is inappropriate for assignment, then InvalidOperationException would be better. If it's something else, please give more details.

Of course, there may not be anything really suitable. Sometimes the best approach really is to write your own exception class.


InvalidOperationException


Sounds like InvalidOperationException to me...


My practice (in the Java world) is to differentiate two cases:

  • You asked me to do something silly - InvalidOperationException or InvalidArgument would work for this

  • I am temporarily broken, if you try again it might work

The second case I can't see a .NET equivalent of my TransientException. Personally I would extend SystemException with a new exception class for that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜