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 3 3351
Hi Scott,
Since this code can be accessed from multiple calling sources it would
be best to maintain a consistent experience. This would be best
accomplished by raising a custom exception and depend on the calling
applications to handle that exception. The calling applications should
also do their own validation to ensure the value is correct and handle
wrong values as appropriate.
I hope this answers your question.
Ian Suttle http://www.iansuttle.com re****************@gmail.com wrote:
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
There is a really nice pattern to cover this situation known as the
notification pattern :) http://www.martinfowler.com/eaaDev/Notification.html
It also handles cases where you may have multiple errors.
Cheers,
Greg Young
MVP - C# http://codebetter.com/blogs/gregyoung
<re****************@gmail.comwrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
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
Ian and Greg,
Thank you both for the quick responses. Your answers were just what I
was looking for.
Scott Huey
Greg Young wrote:
There is a really nice pattern to cover this situation known as the
notification pattern :)
http://www.martinfowler.com/eaaDev/Notification.html
It also handles cases where you may have multiple errors.
Cheers,
Greg Young
MVP - C# http://codebetter.com/blogs/gregyoung
<re****************@gmail.comwrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mark Oueis |
last post by:
I've been struggling with this question for a while. What is better
design? To design functions to return error codes when an error
occures, or to have them throw exceptions.
If you chose the...
|
by: Timothy Graves |
last post by:
I have a quick (pun intended) question for the guru's out there.
I have a piece of code where I am validating the input of chancters
into a cell in a datagrid. I am using the keypressed event to...
|
by: headware |
last post by:
I realize that when making a web application, performing input
validation in the browser is good because it prevents postbacks.
However, input checking that goes beyond making sure a value exists...
|
by: Stephen Witter |
last post by:
I have the following example to catch more than one error. I am new
to error handling in .net so I am wondering if the following is an
exceptable approach:
Sub LinkButton1_Click(sender As Object,...
|
by: dcassar |
last post by:
I have had a lively discussion with some coworkers and decided to get
some general feedback on an issue that I could find very little
guidance on. Why is it considered bad practice to define a...
| |
by: cat |
last post by:
I had a long and heated discussion with other developers on my team on when
it makes sense to throw an exception and when to use an alternate solution.
The .NET documentation recommends that an...
|
by: GiddyUpHorsey |
last post by:
We have a website built on ASP.NET 2.0. We log exceptions in the Error
event of Global.ASAX. We keep a getting number of random exceptions
that make no sense and that we can't reproduce but happen...
|
by: Elliot Rodriguez |
last post by:
I have a form that contains a mix of dynamic controls and declared controls.
All of them are intrinsic .NET controls. Several functions within the page
use Request.Form to query the value of the...
|
by: Sek |
last post by:
Gurus,
I am wondering whether it is right to throw an exception from a
Property of an object.
To get into it further, is it okay to throw exception during 'get'
operation?
I was searching...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |