473,670 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(con tact,e) //catch the exception for the current
object and add it to the list
}
}

if(expList.Coun t>0)
{
//Throw exception and attach the list of exceptions
throw new PartlyPersisten tException("Som e objects failed to be
saved",expList)
}
if(expList.Coun t==Contacts.Cou nt){
//Throw exception and attach the list of exceptions
throw new PersistentFaile dException("All objects failed to be
saved",expList)
}
}
Nov 16 '05 #1
3 1553
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.co m

"Julia" <co********@012 .net.il> wrote in message
news:%2******** **********@TK2M SFTNGP11.phx.gb l...
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(con tact,e) //catch the exception for the current
object and add it to the list
}
}

if(expList.Coun t>0)
{
//Throw exception and attach the list of exceptions
throw new PartlyPersisten tException("Som e objects failed to be
saved",expList)
}
if(expList.Coun t==Contacts.Cou nt){
//Throw exception and attach the list of exceptions
throw new PersistentFaile dException("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.c om> wrote in
message news:u8******** ******@TK2MSFTN GP15.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.co m

"Julia" <co********@012 .net.il> wrote in message
news:%2******** **********@TK2M SFTNGP11.phx.gb l...
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(con tact,e) //catch the exception for the current
object and add it to the list
}
}

if(expList.Coun t>0)
{
//Throw exception and attach the list of exceptions
throw new PartlyPersisten tException("Som e objects failed to be
saved",expList)
}
if(expList.Coun t==Contacts.Cou nt){
//Throw exception and attach the list of exceptions
throw new PersistentFaile dException("All objects failed to be
saved",expList)
}
}


Nov 16 '05 #3
>> if(expList.Coun t==Contacts.Cou nt){

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******** **********@TK2M SFTNGP11.phx.gb l...
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(con tact,e) //catch the exception for the current
object and add it to the list
}
}

if(expList.Coun t>0)
{
//Throw exception and attach the list of exceptions
throw new PartlyPersisten tException("Som e objects failed to be
saved",expList)
}
if(expList.Coun t==Contacts.Cou nt){
//Throw exception and attach the list of exceptions
throw new PersistentFaile dException("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
1718
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 C++, where the use of exceptions as control structures is frowned upon for efficiency reasons. What is the Python canon on this topic ? Are exceptions
5
1380
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 it to the collection, and returns it. The AddNew() method was used to get around the lack of a constructor in VB classes. Now I was just about to rewrite this same pattern in C#, when I realized hey ... I've got constructors, and instead of...
13
4858
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 that when the user presses Ctrl-C or eds the task from task manager (or the kill command in UNIX) then the system should shutdown gracefully. This means it should call the destructors of all objects, which means freeing dynamic memory, closing...
11
1459
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 class and throw that instead -- what would be the usual practise in a case like this?
9
1759
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 improved code readability and performance. Consider an application that must eliminate duplicates in a list. using system.collections;
2
2295
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 there instances that you must use a Do-while loops instead of a for loops or a while loop? or you can use any types of loops in any given problem?
10
3566
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. File "D:\Python24\lib\socket.py", line 295, in read data = self._sock.recv(recv_size) timeout: timed out
1
2182
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 program loops through, reading each contact, it will randomly start throwing exceptions (usually a large batch at a time), therefore fails to read the contact. This is the current code snippet that I am using, although I have tried allsorts. I can't...
43
3661
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 the 21st millenium. This new language not only differs from existing ones by new features and paradigms but which also brings real advantage by rigourously preventing bad programmer habits by omitting features which are outdated. In the proposed...
0
8468
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
8814
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
7415
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
6213
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
4209
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...
0
4390
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
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
2041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1792
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.