tags:

views:

52

answers:

4

If an entry in a web.config file is an invalid value should I throw an exception or set it to a default value when I read it from my application.

For example:

 <add key="enablePasswordReset" value="Invalid">

If my application reads this and wants to store it in a Boolean property, should I throw an exception or set it to the default value of "true"?

A: 

If I were doing it, I would just reset it to the default value, although this could result in some bad hidden bugs down the line if the parsing or saving code broke.

nasufara
+7  A: 

Getting configuration right is so important that I would throw an Exception if it is at all dodgy... better to spot a configuration error as soon as it is made.

  • Scenario "a": developer tries to reconfigure the site; gets a YSOD, reads the error message on their local machine, fixes the error, deploys the change
  • Scenario "b": developer tries to reconfigure the site; it looks OK, deploys the change, somebody notices "day 0" in production, some minor grumbles, it gets fixed by an embarrassed dev
  • Scenario "c": developer tries to reconfigure the site; it looks OK, deploys the change, the difference is subtle and nobody notices until the end-of-year accounts look odd; it goes in front of the board / directors...

I want scenario "a" every time...

Marc Gravell
+3  A: 

I would throw an exception. Better not to run at all than to run with a configuration other than the intended one. Using the default instead could have very severe consequences over time... whereas failing to start (with an obvious error message) will resolve the matter quickly. It's very hard to ignore an application not starting at all.

Jon Skeet
+1  A: 

It depends ;)

Is it something that is likely to happen? If so then trap the error, log it and set a default value.

If it's unlikely, then throw the exception - it shows that something serious has gone wrong.

Given it's the web.config - I'd go with the latter.

ChrisF