473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Should throw exception when validating data?

TS
i'm wondering if it is preferred practice to throw exception in this
circumstance. I have seen it done like that, but i have also read that you
should try to never throw an exception in circumstances where you can do
some test before the operation that would throw the test so that you can by
pass the exception handling which is an expensive operation.

So lets say the scenario is i have an object that is validating itself in
the business layer. When a property doesn't pass validation, i want the UI
to be able to display the failure reason, so the error message that the
business layer sets has to propogate to the UI.

I have seen it done where the object throws exception with special message
which is then caught in UI and displayed to user that way.

Another way i've seen it is the business object simply sets the operation to
return false meaning the validation failed, and it sets a member variable
with the error message so that it can then be accessed in the UI to display
the error message that way.

Any comments on the best practice for doing this?

thanks a bunch
Nov 17 '05 #1
2 2521
You can have versions of the method that do both. If the method call
clearly violates an explicitly documented precondition of the method,
throw an exception. It not, you can return an error structure. You can
design your methods to support "validation preconditions" in which the
supplier validates the preconditions and the caller registers an
interest the the supplier's response.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #2
You can have versions of the method that do both. If the method call
clearly violates an explicitly documented precondition of the method,
throw an exception. It not, you can return an error structure. You can
design your methods to support "validation preconditions" in which the
supplier validates the preconditions and the caller registers an
interest the the supplier's response.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3

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

Similar topics

14
by: inquirydog | last post by:
Hi- One frusterating thing for me with xsl is that I don't know how to make xslt throw some sort of exception when a value-of path does not exist. For instance, suppose I have the following...
16
by: cody | last post by:
I have a method that gets called if the user presses a certain button. If the object is in a state that doesn't allow the calling of that method, what should I do? Should I better throw an...
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
2
by: Johann Gerell | last post by:
Hi all, Since last summer, my group has been putting throw() into the code base as a general guideline where applicable. I've disliked it from the beginning, since I see it as a...
1
by: Barry Anderberg | last post by:
I am using asynch sockets and so I call BeginReceive and then in my callback function I have certain events that would cause me to want to throw an exception. The problem of course is that the...
0
by: TS | last post by:
i'm wondering if it is preferred practice to throw exception in this circumstance. I have seen it done like that, but i have also read that you should try to never throw an exception in...
3
by: Mr Newbie | last post by:
When you write code, you can generally structure it so that you handle all the errors, so when is it most appropriate to Throw an exception rather than coding for error handling in a more granular...
0
by: Steve B. | last post by:
Hi, I'm wondering how to correctly throw exception within ASP.Net pages. I've page wich which waits for an "id" parameter in the querystring. I want to validate this param. I've wrote this...
6
by: jason.cipriani | last post by:
Consider this program, which defines a template class who's template parameter is the type of an exception that can be thrown by members of the class: === BEGIN EXAMPLE === #include...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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 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.