[Python] 快速套用 try-except 到 method 上 (使用 functools)

import functools

def catch_exception(func):
  @functools.wraps(func)
  def wrapper(*args, **kwargs):
    try:
      return func(*args, **kwargs)
    except Exception as e:
      print 'Caught an exception in', f.__name__
  return wrapper

class ...[(...)]:
  @catch_exception
  def calc(self):
    ...
        

 

Last Updated on 2025/01/12 by A1go

Bitnami