Python 从用户定义的异常中删除全名
我已经定义了自己的函数式python异常:Python 从用户定义的异常中删除全名,python,exception,Python,Exception,我已经定义了自己的函数式python异常: class MyError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) 当我提起它时,我得到: Traceback (most recent call last): --snip-- raise MyError("Test") src.exceptions.MyErr
class MyError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
当我提起它时,我得到:
Traceback (most recent call last):
--snip--
raise MyError("Test")
src.exceptions.MyError: 'Test'
如何删除异常名称前的“src.exceptions”,同时保留实际的异常名称,如框内异常?在python中定义自定义异常的最佳方法是从
Exception
派生一个类,然后将该类用作定义用户定义异常的基类
class MyErrorBase(Exception):
pass
class MyError(MyErrorBase):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
try:
raise MyError("Test")
except MyError, exc:
print exc
这将产生如下输出:
测试