something like this will work:
from contextlib import contextmanager
class Test(object):
def __init__(self):
self.j = set()
@contextmanager
def handle_exc(self, msg):
try:
yield
except:
print('adding to internal structure:', msg)
self.j.add(msg)
def test(self):
m = 'snth'
with self.handle_exc(m):
raise Exception('error')
the decorator is difficult to use here because you create the values ββinside the function itself, so the external decorator will never know about them unless you find a way to distribute them (through some kind of exception or something similar).