473,320 Members | 1,858 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,320 software developers and data experts.

Another question regarding exceptions and loops

Thanks for all you responses

assuming I have a collection of objects which I want to save in a database
I wonder if the following is the way to deal with a situation that only some
of the objects were saved

function Save()
{
Exception expList=new Exception() //A list to collect exceptions

foreach(Contact contact in Contacts)
{
try
{
contact.Persist //Save the current contact
}
catch(Exception e)
{
expList.Add(contact,e) //catch the exception for the current
object and add it to the list
}
}

if(expList.Count>0)
{
//Throw exception and attach the list of exceptions
throw new PartlyPersistentException("Some objects failed to be
saved",expList)
}
if(expList.Count==Contacts.Count){
//Throw exception and attach the list of exceptions
throw new PersistentFailedException("All objects failed to be
saved",expList)
}
}
Nov 16 '05 #1
3 1543
Julia,

I think that having separate exceptions for when the list is partially
saved and when it is completely saved is a bad idea, as it will fragment
your code.

Also, what if the operation was an all-or-nothing affair? Where is the
rollback for this kind of thing? Is it in a transaction?

Basically, if there is an exception that is thrown (and I hope you are
not using them for business logic errors), I would store it in a Hashtable,
keyed on the unique identifier for each object (only you know what it is),
and then throw one exception with the hashtable attached to it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Julia" <co********@012.net.il> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
Thanks for all you responses

assuming I have a collection of objects which I want to save in a
database
I wonder if the following is the way to deal with a situation that only
some
of the objects were saved

function Save()
{
Exception expList=new Exception() //A list to collect exceptions

foreach(Contact contact in Contacts)
{
try
{
contact.Persist //Save the current contact
}
catch(Exception e)
{
expList.Add(contact,e) //catch the exception for the current
object and add it to the list
}
}

if(expList.Count>0)
{
//Throw exception and attach the list of exceptions
throw new PartlyPersistentException("Some objects failed to be
saved",expList)
}
if(expList.Count==Contacts.Count){
//Throw exception and attach the list of exceptions
throw new PersistentFailedException("All objects failed to be
saved",expList)
}
}

Nov 16 '05 #2
Thanks

"Also, what if the operation was an all-or-nothing affair? Where is the
rollback for this kind of thing? Is it in a transaction?"

In this case I define a transaction as saving a single Contact not the List
of all contacts
in such a way if some contact failed to be saved(due to validation rules)
I can still try to save other contacts
"I think that having separate exceptions for when the list is partially
saved and when it is completely saved is a bad idea, as it will fragment
your code....
I would store it in a Hashtable,
keyed on the unique identifier for each object (only you know what it is),
and then throw one exception with the hashtable attached to it."

ok,i really dont need separate exceptions

Thanks.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u8**************@TK2MSFTNGP15.phx.gbl...
Julia,

I think that having separate exceptions for when the list is partially
saved and when it is completely saved is a bad idea, as it will fragment
your code.

Also, what if the operation was an all-or-nothing affair? Where is the rollback for this kind of thing? Is it in a transaction?

Basically, if there is an exception that is thrown (and I hope you are
not using them for business logic errors), I would store it in a Hashtable, keyed on the unique identifier for each object (only you know what it is),
and then throw one exception with the hashtable attached to it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Julia" <co********@012.net.il> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
Thanks for all you responses

assuming I have a collection of objects which I want to save in a
database
I wonder if the following is the way to deal with a situation that only
some
of the objects were saved

function Save()
{
Exception expList=new Exception() //A list to collect exceptions

foreach(Contact contact in Contacts)
{
try
{
contact.Persist //Save the current contact
}
catch(Exception e)
{
expList.Add(contact,e) //catch the exception for the current
object and add it to the list
}
}

if(expList.Count>0)
{
//Throw exception and attach the list of exceptions
throw new PartlyPersistentException("Some objects failed to be
saved",expList)
}
if(expList.Count==Contacts.Count){
//Throw exception and attach the list of exceptions
throw new PersistentFailedException("All objects failed to be
saved",expList)
}
}


Nov 16 '05 #3
>> if(expList.Count==Contacts.Count){

The *only* time you will get to this if() is when expList.Count is equal
to zero. (If it is greater than zero, you've thrown and exception and are
out of here. So, the only time this if() will be true, is when
Contacts.Count is also zero, in which case, it's not an error.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"Julia" <co********@012.net.il> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
Thanks for all you responses

assuming I have a collection of objects which I want to save in a database I wonder if the following is the way to deal with a situation that only some of the objects were saved

function Save()
{
Exception expList=new Exception() //A list to collect exceptions

foreach(Contact contact in Contacts)
{
try
{
contact.Persist //Save the current contact
}
catch(Exception e)
{
expList.Add(contact,e) //catch the exception for the current
object and add it to the list
}
}

if(expList.Count>0)
{
//Throw exception and attach the list of exceptions
throw new PartlyPersistentException("Some objects failed to be
saved",expList)
}
if(expList.Count==Contacts.Count){
//Throw exception and attach the list of exceptions
throw new PersistentFailedException("All objects failed to be
saved",expList)
}
}

Nov 16 '05 #4

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

Similar topics

10
by: Olivier Parisy | last post by:
Hi all, I am new to Python (I just finished Guido's tutorial). I was very surprised to learn there that the StopIteration is used to end for loops in a standard iterator setting. I come from...
5
by: John | last post by:
Hi, In my years as a VB programmer, I have settled into this pattern of creating collections classes, with an AddNew() method. AddNew() validates the parameters, instantiates the object, adds...
13
by: Siemel Naran | last post by:
Hi. I posted this question to comp.lang.c++, but am rephrasing it a bit from what I learned and posting to comp.lang.c++.moderated for more insight. So how do I solve my problem? I want it so...
11
by: C# Learner | last post by:
What type of exception should I throw when my code detects that a connection has dropped (i.e. NetworkStream.Read() returns 0)? Should I just throw a SocketException or should I create my own...
9
by: Alvin Bruney [MVP] | last post by:
Exceptions must not be used to control program flow. I intend to show that this statement is flawed. In some instances, exceptions may be used to control program flow in ways that can lead to...
2
by: bitong | last post by:
I'm a little bit confuse with regard to our subject in C..We are now with the Loops..and I was just wondering if given a problem, can you use Do-while loops instead of a for loops or vise versa? are...
10
by: John Nagle | last post by:
Here are three network-related exceptions. These were caught by "except" with no exception type, because none of the more specific exceptions matched. This is what a traceback produced: 1....
1
by: philmarsay | last post by:
I have some code that loops through all contacts in a contact folder (approx 3000), reads some details from each contact and uses that information to perform various functions. However, as the...
43
by: Adem24 | last post by:
The World Joint Programming Language Standardization Committe (WJPLSC) hereby proclaims to the people of the world that a new programming language is needed for the benefit of the whole mankind in...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.