Python:分离模块中的异常工作错误 [英] Python: Exception in the separated module works wrong

查看:271
本文介绍了Python:分离模块中的异常工作错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created new exception class and I like to give it representation of errors like in OSError class. Here is what I want:

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



我该怎么办?如果我继承了基础异常类,我可以这样做吗?这是我试过的(从gnulib工作模块的示例):

What should I do? Can I do this if I inherit from base Exception class? Here is what I've tried (example from module for work with 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的空字符串我怎么能得到我想要的这样的行为?谢谢!

It works wrong and returns just error name for Python 2 and empty string for Python 3. How can I get such behaviour as I want? Thanks!

推荐答案

您应该实现__repr__方法而不是__str __

You should implement __repr__ method instead of __str__

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

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:分离模块中的异常工作错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆