Loop and exception primitive

  • Thread starter Deleted member 43669
  • Start date
Status
Not open for further replies.
D

Deleted member 43669

Guest
I'm noticing more and more that the following:

Code:
errs = []
for element in elements:
  try:
    do_stuff(element)
  except err:
    errs += err
if errs:
  raise Exception(errs)

or variations thereof is one of the most common reasons to write an except block in my code.

In my case, I believe this points me to the use of Celery: do_stuff should be a task invoked asynchronously and Celery should handle retrying/logging/notification of errors.

Do you see the above pattern a lot? Is it an anti-pattern? How do you usually handle it?

Cheers,

Álex
 

QuadDamaged

Ars Praefectus
3,983
Subscriptor++
I generally encounter this kind of aggregated exception handling when dealing with async/parallel tasks, but the framework generally abstracts this away from the caller (.Net defines an AggregatedException type).

If your framework of choice doesn't already do it, you might roll your own version of map(fn,elems) which does exactly what you are currently doing.
 
Status
Not open for further replies.