0
def process_item(...):
  try:
    #some hacking
    #...
    #lots of buggy code
  except Exception as e:
    #do logging
  #record complete/incomplete state

Yes I know that I should generally intercept specific exceptions. But if the code is applied to many items which come from input data. So there might be unpredictable errors. Programming errors are also possible. I have special mechanisms to log/report error states. I don't want to stop the overall process on an unexpected error, whichever the error nature is. I rather want to process as many items as possible. Can I catch base Exception as in above?

noname7619
  • 3,370
  • 3
  • 21
  • 26
  • What is your question exactly? Your use case isn't very clear in my opinion. Can you show a minimal example of exactly what kind of input and what kind of code lies in the `try` block? – Chris_Rands Jul 24 '17 at 11:43
  • 1
    im not sure if you are trying to ask this: https://stackoverflow.com/questions/18982610/difference-between-except-and-except-exception-as-e-in-python – Moyote Jul 24 '17 at 11:46

1 Answers1

2

Of course, and the reasons you provided are excellent cases in which you would do just that. Sometimes you don't have anything special to do with the exception, but you still don't want to let it go on, whatever type of exception it is (this is also very common in apps that need to run forever, such as servers. Many errors would be caught and logged, and the system will just try its best to continue running as best as it can.

Do, however, note the difference between except Exception [as e]: and except:, as described in this Q&A: Difference between except: and except Exception as e: in Python

Eran Zimmerman Gonen
  • 4,375
  • 1
  • 19
  • 31