views:

96

answers:

5

I am writing some data access code and I want to check for potentially "invalid" data states in the database. For instance, I am returning a widget out of the database and I only expect one. If I get two, I want to throw an exception. Even though referential integrity should prevent this from occurring, I do not want to depend on the DBAs never changing the schema (to clarify this, if the primary key constraint is removed and I get a dupe, I want to break quickly and clearly).

I would like to use the System.IO.InvalidDataException, except that I am not dealing with a file stream so it would be misleading. I ended up going with a generic applicationexception. Anyone have a better idea?

A: 

You could write a custom exception if you do not find any suitable standard-exception ...

But, you say:

Even though referential integrity should prevent this from occurring, I do not want to depend on the DBAs never changing the schema.

When someone changes the DB schema, changes are pretty big that you'll have to make some modifications to your application / data-access code as well ...

Frederik Gheysels
Err, I said that wrong. I just want the error to quickly surface is the schema is changed out from under me. I know I could create a custom exception, but I wanted to to make sure I wasn't missing a good default.
jslatts
+5  A: 

If you need an exception that would exactly describe the situation you're dealing with, why not make your own exception?

Just inherit it from System.Exception.

VexXtreme
I was just trying to follow Brad Abram's advice and wanted to make sure I wasn't missing an appropriate built in exception: http://blogs.msdn.com/brada/archive/2005/03/27/402801.aspx
jslatts
+1 Creating your own Exception would be my advice too - it' your friend when tracking down errors. Even more so when it comes to boundaries and constraints tied to your application or business logic.
Filburt
+1  A: 

I might be tempted to use one of the following:

InvalidConstraintException
NotSupportedException
OverflowException

Or, just go ahead and create my own: TooManyRowsException

Chris Lively
That makes it sound like the constraint itself is invalid - ViolatedConstraintException or ConstraintViolationException?
Jon Skeet
@Jon: In a way it is a constraint that has been violated. In his case the constraint exists, he's just afraid of the database guy dropping it.
Chris Lively
A: 

ApplicationException is a good one as it indicates that it hasnt come from the framework itself.

James Westgate
There's an FxCop rule that says you should not throw ApplicationException, or derive from it ...
Frederik Gheysels
"Do not throw or derive from System.ApplicationException." http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx
LukeH
Hey interesting I didnt know this. So best then to define your own exception from system.exception.
James Westgate
+8  A: 

InvalidDataException seems pretty reasonable to me:

  • The name fits perfectly
  • The description fits pretty reasonably when you consider that it's effectively a data "stream" from the database
  • Nothing in the description mentions files, so I wouldn't be worried about that side of things

You're effectively deserializing data from a store. It happens to be an RDBMS, but that's relatively unimportant. The data is invalid, so InvalidDataException fits well.

To put it another way - if you were loading the data from a file, would you use InvalidDataException? Assuming you would, why should it matter where the data is coming from, in terms of the exception being thrown?

Jon Skeet
Yes, I would use it for loading data from a file--I agree with your logic. I'll go ahead and use InvalidDataException.
jslatts
This is probably indeed the most suitable existing exception class. It just feels weird IMHO -- despite @Jon Skeet's data stream argument -- to throw an exception from the `System.IO` namespace when you do `System.Data`-related things. (Of course, the concrete meaning of namespaces shouldn't be overestimated, but it still conveys a subtle message that it might not be *quite* the right thing to throw.) I think that in this case, providing a custom exception class would be justified.
stakx
Personally Im against throwing native framework exceptions from non framework code for the reason that support requests may not be directed correctly. Perhaps a a subclass called ApplicationInvalidDataException would be appropriate?
James Westgate
That is in interesting perspective. I can certainly see the value in being able to setup processes/escalation paths for custom exceptions. I'll have to give that some though.
jslatts