Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Should I throw an exception

Status
Not open for further replies.

Craig0201

Technical User
Oct 11, 2000
1,261
GB
Hi all,

Advice needed.

I'm setting a property in an object that can only accept certain values. How do I respond if the value is set to on of the disallowed values? At the moment, I am throwing an ArgumentOutOfRangeException but this seems like a silly idea as the attempted entry of these values will be common. But if I do that how do I respond? Should I set a flag on the object and check it?

Craig
 
It depends on what layer of your application you're at. If you're on a WinForm, then a input validator of some kind would be best. If you're in a data layer, then throwing the ArgumentOutOfRange exception is correct, as by this point the usual stupid-user errors should have been caught, and this is the last-chance place to catch something wrong.

Chip H.
 
Chip,

Unfortunately a mix of the two. I'm checking against a database for a number which (in theory) should be unique and in the database is unique. However, the number is preprinted on paper and isn't! Hence users will genuinely type the number correctly and an error must still be given.

Further advice?

Craig
 
Hey Chip,

Here's what we did at the last place I worked to solve a similar problem:

We wanted to be able to display a sql exception error to the user, but not crash the system (i.e. if they tried to delete a user, but data integrity of the database would be violated and a sql exception was thrown, we didn't want the whole app crashing). So what we did was make all sql exceptions of a custom exception class we made. When our asp.net forms caught one of these, instead of forwarding to an error page it would display a messagebox explaining the error. The user could click ok, and continue on in the program.

A similar thing would work here for you. If you created a custom "data integrity exception" or something, and added a specific catch in your presentation code to look for it, you could just have a messagebox pop up saying "the number entered is not in the database" or what not. That way the users can continue on without crashing the app.

hth

D'Arcy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top