Skip to main content
added 174 characters in body
Source Link
Doc Brown
  • 220.5k
  • 35
  • 410
  • 625

Your solution looks ok to me. Alternatively (not necessarily "better"), here is a DRY version without using isinstance:

lasterr=None try: some_func() except FirstException as err: # known possible err # log err lasterr=err except SecondException as err: # known possible err # log err lasterr=err except Exception as err: # unexpected err to prevent crash # log err lasterr=err finally: if lasterr is not None: notify_user(err) # close logs 

Of course, the statement lasterr=err is repeated here, but since this does not contain any real "logic", this usually does not count as a DRY violation. Those statements are usually not as likely to change as a function call notify_user(err), so the goal of DRY to reduce the number of places where changes will happen is fulfilled.

You did not show us the logging code, but it might be also a good idea not to do the real logging at all places where # log err occurs, only remember the specific logging message in a variable, and do the logging in the finally section as well.

Your solution looks ok to me. Alternatively (not necessarily "better"), here is a DRY version without using isinstance:

lasterr=None try: some_func() except FirstException as err: # known possible err # log err lasterr=err except SecondException as err: # known possible err # log err lasterr=err except Exception as err: # unexpected err to prevent crash # log err lasterr=err finally: if lasterr is not None: notify_user(err) # close logs 

Of course, the statement lasterr=err is repeated here, but since this does not contain any real "logic", this usually does not count as a DRY violation.

Your solution looks ok to me. Alternatively (not necessarily "better"), here is a DRY version without using isinstance:

lasterr=None try: some_func() except FirstException as err: # known possible err # log err lasterr=err except SecondException as err: # known possible err # log err lasterr=err except Exception as err: # unexpected err to prevent crash # log err lasterr=err finally: if lasterr is not None: notify_user(err) # close logs 

Of course, the statement lasterr=err is repeated here, but since this does not contain any real "logic", this does not count as a DRY violation. Those statements are usually not as likely to change as a function call notify_user(err), so the goal of DRY to reduce the number of places where changes will happen is fulfilled.

You did not show us the logging code, but it might be also a good idea not to do the real logging at all places where # log err occurs, only remember the specific logging message in a variable, and do the logging in the finally section as well.

Source Link
Doc Brown
  • 220.5k
  • 35
  • 410
  • 625

Your solution looks ok to me. Alternatively (not necessarily "better"), here is a DRY version without using isinstance:

lasterr=None try: some_func() except FirstException as err: # known possible err # log err lasterr=err except SecondException as err: # known possible err # log err lasterr=err except Exception as err: # unexpected err to prevent crash # log err lasterr=err finally: if lasterr is not None: notify_user(err) # close logs 

Of course, the statement lasterr=err is repeated here, but since this does not contain any real "logic", this usually does not count as a DRY violation.