473,785 Members | 2,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception or validation method?

Hi all,

This is more of a design question. I'm building an n-tier application,
and the question revolves around the buisness layer telling the
application that a value is not appropriate for whatever reason.

What is the recommended way of doing this?

Thrown an exception of a property is assigned a bad value? Throw an
exception when a method is called that needs to use that value? Create
a method that will indicate via a return code if the data is valid (and
if thats how to do things, how do you tell the application which value
is incorrect)?

What if for one application you need to validate the field as soon as
the user tries to move to the next field, and another (like an asp.net
page) you valid the record when save is click?

Thanks for any suggestions!
Andy

Nov 17 '05 #1
7 3102
Hi Andy:

This is always a sticky issue, but I think that you need to validate as soon
as you possibly can. If you have a user interface element that receives
input and you always know what the bounds of that input are, the user should
not be able to leave the interface element with improper data. However, if
your interface is more general and may apply in multiple input situations
where the bounds of the input are not known until the input is moved to the
next component in line (like business logic), you might wait to validate a
whole group of data at once and return to the user with a report. This would
make sense if other factors come in to play, like balancing immediate
validation vs. the cost of a trip across the network. Unless you are dealing
in a situation where a system is unattended by a user or it is only checked
periodically, I would not validate data by having its use cause a failure.
It's just one more thing to go wrong.

"Andy" <aj********@cap citypress.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hi all,

This is more of a design question. I'm building an n-tier application,
and the question revolves around the buisness layer telling the
application that a value is not appropriate for whatever reason.

What is the recommended way of doing this?

Thrown an exception of a property is assigned a bad value? Throw an
exception when a method is called that needs to use that value? Create
a method that will indicate via a return code if the data is valid (and
if thats how to do things, how do you tell the application which value
is incorrect)?

What if for one application you need to validate the field as soon as
the user tries to move to the next field, and another (like an asp.net
page) you valid the record when save is click?

Thanks for any suggestions!
Andy

Nov 17 '05 #2
Thanks Howard, your answer does make sense.

So to be clear from the last few lines, you think that the objects
should expose some method that will check the data and return data
indicating what ones are valid, and not throw an exception?

Something else comes to mind..

Whats your opinion on when an exception is thrown in say the data
layer?

I would think that as each layer catches an exception, it would wrap it
in a general exception (or more specific one, if need be) and rethrow
that.

My thinking is that if the application should ONLY know about the
business layer, it should only get exceptions defined in that layer
(althought inner exceptions would be from lower layers). So that the
business layer should throw a new exception, with the data layer
exception as an inner exception.

Any thoughts around that?

Thanks!
Andy

Nov 17 '05 #3
Andy,

The way I think of it is like this. Exceptions are for exceptional
cases, and business logic is not one of those cases. Also, because the
frequency of incorrect data being entered is high compared to other
exceptional cases, you can run into a performance issue (since
throwing/catching an exception is much slower than checking a value, for
instance).

Business logic is just validation and operations. An exception should
be thrown IMO only when there is a condition or situation that is unexpected
which isn't planned for. Someone entering the wrong value somewhere isn't
one of those cases.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Andy" <aj********@cap citypress.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hi all,

This is more of a design question. I'm building an n-tier application,
and the question revolves around the buisness layer telling the
application that a value is not appropriate for whatever reason.

What is the recommended way of doing this?

Thrown an exception of a property is assigned a bad value? Throw an
exception when a method is called that needs to use that value? Create
a method that will indicate via a return code if the data is valid (and
if thats how to do things, how do you tell the application which value
is incorrect)?

What if for one application you need to validate the field as soon as
the user tries to move to the next field, and another (like an asp.net
page) you valid the record when save is click?

Thanks for any suggestions!
Andy

Nov 17 '05 #4
yeah, but I hate it when all my methods now have to have some error code as
return value, and I'd have to map the value to find out what caused it.
feels so antiquated.

maybe they need something in between, something not quite as heavy as
exceptions but still has the throw-catch paradigm. I just find it much more
convenient to deal with errors this way.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Andy,

The way I think of it is like this. Exceptions are for exceptional
cases, and business logic is not one of those cases. Also, because the
frequency of incorrect data being entered is high compared to other
exceptional cases, you can run into a performance issue (since
throwing/catching an exception is much slower than checking a value, for
instance).

Business logic is just validation and operations. An exception should
be thrown IMO only when there is a condition or situation that is unexpected
which isn't planned for. Someone entering the wrong value somewhere isn't
one of those cases.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Andy" <aj********@cap citypress.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hi all,

This is more of a design question. I'm building an n-tier application,
and the question revolves around the buisness layer telling the
application that a value is not appropriate for whatever reason.

What is the recommended way of doing this?

Thrown an exception of a property is assigned a bad value? Throw an
exception when a method is called that needs to use that value? Create
a method that will indicate via a return code if the data is valid (and
if thats how to do things, how do you tell the application which value
is incorrect)?

What if for one application you need to validate the field as soon as
the user tries to move to the next field, and another (like an asp.net
page) you valid the record when save is click?

Thanks for any suggestions!
Andy


Nov 17 '05 #5
"=?Utf-8?B?RGFuaWVsIEp pbg==?=" <Da*******@disc ussions.microso ft.com>
wrote in news:C1******** *************** ***********@mic rosoft.com:
yeah, but I hate it when all my methods now have to have some error
code as return value, and I'd have to map the value to find out what
caused it. feels so antiquated.

maybe they need something in between, something not quite as heavy as
exceptions but still has the throw-catch paradigm. I just find it
much more convenient to deal with errors this way.


It all depends on your paradigm. Exceptions are not always errors and are very valid returns for
validation errors, especially when process boundaries are crossed. Take a look at various FCL
Parse etc, and these dont even cross process boundaries.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
Nov 17 '05 #6
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard .caspershouse.c om> wrote:
The way I think of it is like this. Exceptions are for exceptional
cases, and business logic is not one of those cases. Also, because the
frequency of incorrect data being entered is high compared to other
exceptional cases, you can run into a performance issue (since
throwing/catching an exception is much slower than checking a value, for
instance).
The performance is *very* unlikely to be significantly affected by
throwing an exception. Even if you were to throw 100,000 exceptions in
an hour (which would be very unlikely, IMO) that would still only take
up a tiny proportion of the processing power of any server which is
likely to handle that many requests.

(If it were *so* crucial not to throw exceptions, ASP.NET no doubt
wouldn't do so when HttpResponse.Re direct is called...)

Personally I like exceptions in terms of their fail-fast nature -
they're a good way of making it very hard to ignore the problem. It's
all too easy to ignore return values (just look at the number of people
who have bugs due to neglecting the return value of Stream.Read).
Business logic is just validation and operations. An exception should
be thrown IMO only when there is a condition or situation that is unexpected
which isn't planned for. Someone entering the wrong value somewhere isn't
one of those cases.


I think it really depends on the situation. Personally I like to
provide two methods, one of which will throw an exception and one of
doesn't. That way in the (IME) rare situation where you're likely to be
validating hundreds of thousands of entries without doing much else,
you can avoid the performance hit of exceptions, and likewise when you
don't want the "fail fast" behaviour.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

"Andy" <aj********@cap citypress.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Thanks Howard, your answer does make sense.

So to be clear from the last few lines, you think that the objects
should expose some method that will check the data and return data
indicating what ones are valid, and not throw an exception?

I think a multi-tiered system should be checking on if the data is valid as
soon as it can.

I think throwing an exception vs indicating validity depends on the data.
For example in a system I am working on a user can organize a subset of data
into groupings. The original data may change and then the groupings may no
longer be valid. In this case I want the user to see an indication of the
validity of their groupings and be able to doctor them according to the new
base data. It is important to indicate validity in this case. The system is
designed with this in mind. No exception is thrown if a user opens his
grouping in the system, but validity is indicated. If for example, a value
of 101 is entered where a percentage is expected then I would throw an
exception.

*** An important thing to consider too, is if you throw an exception,
execution will pick up at the exception handler. You might encounter an
instance where this is not the desired outcome. You might want to inform the
user through a modal dialog that an error has occurred and then continue on
in your code handling the error condition.
Something else comes to mind..

Whats your opinion on when an exception is thrown in say the data
layer?

As long as it is consistent and well documented I don't have a problem with
it. Consistent exception handling has been one of the greatest joys of my
..Net experience.
I would think that as each layer catches an exception, it would wrap it
in a general exception (or more specific one, if need be) and rethrow
that.

My thinking is that if the application should ONLY know about the
business layer, it should only get exceptions defined in that layer
(althought inner exceptions would be from lower layers). So that the
business layer should throw a new exception, with the data layer
exception as an inner exception.

Any thoughts around that?
I think that the end user should never see an error message that is not
based in the domain of the application. However, they still need to be able
to alert technical support as to the nature of the error they receive in
some way. This also holds true for a programmer that receives an exception
from a subsystem. He should recieve information from the subsystem he was
addressing but also needs to address or pass on the root cause of the error
so that it can eventually be resolved. I still think that there is a little
area to fudge this amongst the developers, as long as the end user isn't
confronted with some error like:

Unhandled exception at 0xABCD1234, deleteing all your work. ;-)

Thanks!
Andy

Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
4229
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
3
1730
by: gemel | last post by:
I am fairly comfortable with much of the .NET Exception mechanism, but I'm a bit unsure of some areas. 1. When an exception occurs and the exception is not caught then Exception processing will continue at the next Catch block up the stack. When this happens will the 'Finally' block of the original 'Try/Catch' be executed? 2. If I wish to recover from the error that caused the Exception can I put the Try/Catch in a loop so that I can...
28
2089
by: jakk | last post by:
Hello All, I have a question about how to handle exceptions. Iam working on an ASP.NET application which has a presentation layer, Business Layer and DataAccess Layer. Iam confused about where to handle exceptions. Say forexample I have a requirement where certain value has to be between 30-72 only and Iam doing this validation in the the BL. if the value is not between 30-72 then Iam throwing an exception back to the UI layer. Is this...
1
5435
by: angus | last post by:
Dear All, how to try-catch "A potentially dangerous Request.Form value was detected from the client (txtUserName="<asdf")." this exception? i've set the debugger in the Page_InIt function, but this page is shown before the Page_init function. i know that i can set validateRequest="false" in my page, but how can i
3
2738
by: Sean Tynan | last post by:
I want to find out the best way for a method to notify calling code of situations such as validation errors, etc. that may occur during method execution. e.g. say I have a method (business logic layer) that inserts a user into a database, and returns the newly created UserID: public function CreateUser(userName as string, userEmail as string) dim userID as integer
7
1709
by: ZorpiedoMan | last post by:
Well, I'm still wondering around in the "land of the lost exception"... It seems that controls that are bound to a class and the class throws an error in the SET method of the bound member, the exception cannot be caught. I have wrapped my entire application in all the global exception handlers I can find, and still to no avail. In fact all the Try/Catches and Exception event handlers are worthless... Put on your advanced thinking...
3
3407
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own exception constructors? I've noticed that, f.ex., ArgumentException accepts null arguments without raising ArgumentNullException. Obviously, if nothing is to be supplied to the exception constructor, the default constructor should
3
5059
by: Ken Shimizu | last post by:
I get an error when submitting an .ASPX page in Safari beta, MAC and PC document.getElementbyId(‘FolderBrowser’).submit(); After the server side code has executed without problems I get the following error. Server Error in '/FileWayTest' Application. -------------------------------------------------------------------------------- Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that...
1
2027
by: Ami | last post by:
We have an application where in SOAP is used as a middle layer to communication between Java Front layer and DCE( Distributed Computing Env) backend layer. We are using Systinet Server for C++ to deploy our SOAP code and we make DCE RPC call through our SOAP code in C++ to connect and SEND and Retrieve Data from DCE. Now we have to incorporate exception handling in such a way , if DCE is down, then while making a DCERPC call SOAP Server...
0
9645
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
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 we have to send another system
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.