37,721
社区成员
发帖
与我相关
我的任务
分享
#a.py
def func1():
try:
...
except:
raise
#b.py
import a
def func2():
try:
...
func1()
...
except:
#此处如果发生了异常 如何判断此异常是a.py中引起的还是b.py中引起的呢
#我自己写了一个异常的类,如下所示
class MetaError(Exception):
def __init__(self,msg,line):
self.msg = msg
self.line = line
self.className = str(self.__class__)[str(self.__class__).rfind(".")+1:str(self.__class__).rfind("'")]
class MetaManagerError(MetaError):
def __init__(self,msg,line):
super(MetaManagerError,self).__init__(msg,line)
class MysqlDaoError(MetaError):
def __init__(self,msg,line):
super(MysqlDaoError,self).__init__(msg,line)
class CustomError(MetaError):
def __init__(self,msg,line):
super(CustomError,self).__init__(msg,line)
#在MysqlDao.py中有如下语句
def func1():
try:
...
except:
#出现异常时,抛出MetaError.MysqlDaoError类异常
self.conn.rollback()
exInfo = sys.exc_info()
raise MetaError.MysqlDaoError,(exInfo[1],exInfo[2].tb_lineno)
#在MetaManager.py中会调用在MysqlDao.func1(),此时如果出错了在
def func2():
try:
...
MysqlDao.func1()
...
except Exception,e:
raise MetaError.MetaManagerError,(e.msg,e.line)