gpt4 book ai didi

Python:分离模块中的异常工作错误

转载 作者:行者123 更新时间:2023-12-03 08:35:18 26 4
gpt4 key购买 nike

我创建了新的异常类,我喜欢给它表示错误,就像在 OSError 类中一样。这是我想要的:

>>> raise(MyError(1, 'info'))
MyError: [Errno 1] predefined text: info

我该怎么办?如果我从基 Exception 类继承,我可以这样做吗?这是我尝试过的(使用 gnulib 的模块示例):
class GNULibError(Exception):
'''Exception handler for GNULib classes.'''

def __init__(self, errno, errinfo=None):
'''Each error has following parameters:
errno: code of error; used to catch error type
1: destination directory does not exist: <destdir>
2: configure file does not exist: <configure.ac>
3: selected module does not exist: <module>
4: <cache> is expected to contain gl_M4_BASE([m4base])
5: missing sourcebase argument
6: missing docbase argument
7: missing testsbase argument
8: missing libname argument
errinfo: additional info'''
self.errno = errno; self.errinfo = errinfo
self.args = (self.errno, self.errinfo)

def __str__(self):
errors = \
[ # Begin list of errors
"destination directory does not exist: %s" % self.errinfo,
"configure file does not exist: %s" % self.errinfo,
"selected module does not exist: %s" % self.errinfo,
"%s is expected to contain gl_M4_BASE([%s])" % \
(os.path.join(self.errinfo, 'gnulib-comp.m4'), self.errinfo),
"missing sourcebase argument; cache file doesn't contain it,"
+" so you might have to set this argument",
"missing docbase argument; you might have to create GNULibImport" \
+" instance with mode 0 and docbase argument",
"missing testsbase argument; cache file doesn't contain it,"
+" so you might have to set this argument"
"missing libname argument; cache file doesn't contain it,"
+" so you might have to set this argument",
"dependencies and testflag 'default' cannot be used together",
] # Complete list of errors
if not PYTHON3:
self.message = (b'[Errno %d] %s' % \
(self.errno, errors[self.errno -1].encode(ENCS['default'])))
else: # if PYTHON3
self.message = ('[Errno %d] %s' % \
(self.errno, errors[self.errno -1]))
return(self.message)

它工作错误,只返回 Python 2 的错误名称和 Python 3 的空字符串。我怎样才能得到我想要的这种行为?谢谢!

最佳答案

您应该实现 __repr__ 方法而不是 __str__

http://docs.python.org/reference/datamodel.html#object.__repr__

那可行:

 class GNULibError(Exception):
'''Exception handler for GNULib classes.'''

def __init__(self, errno, errinfo=None):
'''Each error has following parameters:
errno: code of error; used to catch error type
1: destination directory does not exist: <destdir>
2: configure file does not exist: <configure.ac>
3: selected module does not exist: <module>
4: <cache> is expected to contain gl_M4_BASE([m4base])
5: missing sourcebase argument
6: missing docbase argument
7: missing testsbase argument
8: missing libname argument
errinfo: additional info'''
self.errno = errno; self.errinfo = errinfo
self.args = (self.errno, self.errinfo)

def __repr__(self):
errors = \
[ # Begin list of errors
"destination directory does not exist: %s" % self.errinfo,
"configure file does not exist: %s" % self.errinfo,
"selected module does not exist: %s" % self.errinfo,
"%s is expected to contain gl_M4_BASE([%s])" % \
(os.path.join(self.errinfo, 'gnulib-comp.m4'), self.errinfo),
"missing sourcebase argument; cache file doesn't contain it,"
+" so you might have to set this argument",
"missing docbase argument; you might have to create GNULibImport" \
+" instance with mode 0 and docbase argument",
"missing testsbase argument; cache file doesn't contain it,"
+" so you might have to set this argument"
"missing libname argument; cache file doesn't contain it,"
+" so you might have to set this argument",
"dependencies and testflag 'default' cannot be used together",
] # Complete list of errors
if not PYTHON3:
self.message = (b'[Errno %d] %s' % \
(self.errno, errors[self.errno -1].encode(ENCS['default'])))
else: # if PYTHON3
self.message = ('[Errno %d] %s' % \
(self.errno, errors[self.errno -1]))
return(self.message)

关于Python:分离模块中的异常工作错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11173945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com