Another question regarding exceptions and loops

  • Thread starter Thread starter Julia
  • Start date Start date
J

Julia

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)
}
}
 
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.
 
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 said:
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]
- (e-mail address removed)

Julia said:
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)
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top