tags:

views:

56

answers:

4

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)

+2  A: 

Sounds like InvalidOperationException to me...

Jason Punyon
+3  A: 

InvalidOperationException

Darin Dimitrov
+3  A: 

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.

Jon Skeet
It's because the project is in an inappropriate state. Thanks !
PaN1C_Showt1Me
A: 

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.

djna