Python continue from the point where exception was thrown -
hi there way continue point exception thrown? eg have following psudo code
unique code 1 unique code 2 unique code 3
if want ignore exceptions of of unique code statements have this:
try: #unique code 1 except: pass try: #unique code 2 except: pass try: #unique code 3 except: pass
but isn't elegant me, , life of me can't remember how resolved kind of problem last time... want have like
try: unique code 1 unique code 2 unique code 3 except: continue last exception raised
updated: reason: reason asking above 3 lines of code share same kind of exceptions, lets say, extract information array, , in particular scenario don't care exception of value not in array.
wrap each of code sections function , try calling each in for-loop:
def code1(): #unique code 1 def code2(): #unique code 2 def code3(): #unique code 3 section in [code1, code2, code3]: try: section() except: pass
edit: if still looking way, use decorator make each call "safe" buy returning errors instead of throwing them. not sure if pythonic or great way things way:
def safe_call(fn): def wrapper(*args, **kwargs): try: return fn(*args, **kwargs) except exception error: return error return wrapper @safe_call def code1(): #unique code 1 @safe_call def code2(): #unique code 2 @safe_call def code3(): #unique code 3 code1() code2() code3()
Comments
Post a Comment