R
redefined.horizons
I'm new to C#, (coming from a Java programming background), and I have
a question about the correct use of exceptions. My book "Programming
C#" by O'Reilly says the following in Chapter 11, "Handling
Exceptions", on page 245:
"An error is caused by user action. For example, the user might enter a
number where a letter is expected. Once again, an error might cause an
exception, but you can still prevent that by catching errors with
validation code. Whenever possible, errors should be anticipated and
prevented."
I have a situation where I want to set an integer property of a C#
class to a value between 1 and 9. I was going to throw exceptions if
the user passed in an int value to the setter of the property that was
less than one or more than 9. Now I am not sure this is the correct
thing to do. I could just pass a message out to the console explaining
the invalid input, but if the setter was called by another object, and
not as the result of a direct user action I think an exception with a
stack trace would be more appropriate.
However, I won't know when the setter for the property is being acessed
via direct interaction with the user, or programattically by another
object.
What is the best practice for C# development in this situation. Do I
throw an exception, or do I write some validation code? What would I
put in the validation code in this particular situation?
Thanks,
Scott Huey
a question about the correct use of exceptions. My book "Programming
C#" by O'Reilly says the following in Chapter 11, "Handling
Exceptions", on page 245:
"An error is caused by user action. For example, the user might enter a
number where a letter is expected. Once again, an error might cause an
exception, but you can still prevent that by catching errors with
validation code. Whenever possible, errors should be anticipated and
prevented."
I have a situation where I want to set an integer property of a C#
class to a value between 1 and 9. I was going to throw exceptions if
the user passed in an int value to the setter of the property that was
less than one or more than 9. Now I am not sure this is the correct
thing to do. I could just pass a message out to the console explaining
the invalid input, but if the setter was called by another object, and
not as the result of a direct user action I think an exception with a
stack trace would be more appropriate.
However, I won't know when the setter for the property is being acessed
via direct interaction with the user, or programattically by another
object.
What is the best practice for C# development in this situation. Do I
throw an exception, or do I write some validation code? What would I
put in the validation code in this particular situation?
Thanks,
Scott Huey