1

For my exception handling I actually want to have a concept of a finally statement which is only called when an exception happened (and not like the real finally statement which is called in all cases).

Here is a short sketch:

class ErrorA(Exception):
    pass

class ErrorB(Exception):
    pass

error_counter = 0

try:
    raise ErrorA # or raise ErrorB
except ErrorA:
    print('ErrorA')
    error_counter += 1
except ErrorB:
    print('ErrorB') # some error handling for ErrorB
    error_counter += 1

I need to react on every Exception differently (in this sketch represented by print)

Is there something more pythonic? For me it looks weird that I need error_counter += 1 in all Exception cases.

Michael Dorner
  • 17,587
  • 13
  • 87
  • 117
  • 1
    You could change to `except Exception as exc:` then use `if isinstance(exc, ...)` within that to group behaviour based on specific error types. – jonrsharpe Jul 12 '18 at 07:32
  • Would `ok = False; try: ..... ok = True;` then after `except`, `if not ok: error_counter += 1` be more Pythonic for you? Or you can wrap and extra `try: ... except Exception: error_counter += 1; raise` – Amadan Jul 12 '18 at 07:32

0 Answers0