在 Python 中使用 isinstance 检查特定类型的异常是否合理?

发布于 2025-01-05 01:35:06 字数 640 浏览 0 评论 0原文

在Python中捕获通用异常,然后使用isinstance()来检测特定类型的异常以便适当地处理它是否合理?

我现在正在使用 dnspython 工具包,它有一系列异常,例如超时、NXDOMAIN 响应等。这些异常是 dns.exception.DNSException 的子类,因此我想知道捕获 DNSException 然后使用 isinstance() 检查特定异常是否合理或符合 Python 风格。

例如,

try:
    answers = dns.resolver.query(args.host)
except dns.exception.DNSException as e:
    if isinstance(e, dns.resolver.NXDOMAIN):
        print "No such domain %s" % args.host
    elif isinstance(e, dns.resolver.Timeout):
        print "Timed out while resolving %s" % args.host
    else:
        print "Unhandled exception"

我是 Python 新手,所以要温柔!

Is it reasonable in Python to catch a generic exception, then use isinstance() to detect the specific type of exception in order to handle it appropriately?

I'm playing around with the dnspython toolkit at the moment, which has a range of exceptions for things like a timeout, an NXDOMAIN response, etc. These exceptions are subclasses of dns.exception.DNSException, so I am wondering if it's reasonable, or pythonic, to catch DNSException then check for a specific exception with isinstance().

e.g.

try:
    answers = dns.resolver.query(args.host)
except dns.exception.DNSException as e:
    if isinstance(e, dns.resolver.NXDOMAIN):
        print "No such domain %s" % args.host
    elif isinstance(e, dns.resolver.Timeout):
        print "Timed out while resolving %s" % args.host
    else:
        print "Unhandled exception"

I'm new to Python so be gentle!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宣告ˉ结束 2025-01-12 01:35:06

这就是多个 except 子句的用途:

try:
    answers = dns.resolver.query(args.host)
except dns.resolver.NXDOMAIN:
    print "No such domain %s" % args.host
except dns.resolver.Timeout:
    print "Timed out while resolving %s" % args.host
except dns.exception.DNSException:
    print "Unhandled exception"

请注意子句的顺序:将采用第一个匹配的子句,因此将对超类的检查移至末尾。

That's what multiple except clauses are for:

try:
    answers = dns.resolver.query(args.host)
except dns.resolver.NXDOMAIN:
    print "No such domain %s" % args.host
except dns.resolver.Timeout:
    print "Timed out while resolving %s" % args.host
except dns.exception.DNSException:
    print "Unhandled exception"

Be careful about the order of the clauses: The first matching clause will be taken, so move the check for the superclass to the end.

浅暮の光 2025-01-12 01:35:06

您可以从 dns.resolver 导入一些异常。
(未经测试的代码)

from dns.resolver import Resolver, NXDOMAIN, NoNameservers, Timeout, NoAnswer

try
    host_record = self.resolver.query(self.host, "A")
    if len(host_record) > 0:
        Mylist['ERROR'] = False
        # Do something

except NXDOMAIN:
    Mylist['ERROR'] = True
    Mylist['ERRORTYPE'] = NXDOMAIN
except NoNameservers:
    Mylist['ERROR'] = True
    Mylist['ERRORTYPE'] = NoNameservers
except Timeout:
    Mylist['ERROR'] = True
    Mylist['ERRORTYPE'] = Timeout
except NameError:
    Mylist['ERROR'] = True
    Mylist['ERRORTYPE'] = NameError

From dns.resolver you can import some exceptions.
(untested code)

from dns.resolver import Resolver, NXDOMAIN, NoNameservers, Timeout, NoAnswer

try
    host_record = self.resolver.query(self.host, "A")
    if len(host_record) > 0:
        Mylist['ERROR'] = False
        # Do something

except NXDOMAIN:
    Mylist['ERROR'] = True
    Mylist['ERRORTYPE'] = NXDOMAIN
except NoNameservers:
    Mylist['ERROR'] = True
    Mylist['ERRORTYPE'] = NoNameservers
except Timeout:
    Mylist['ERROR'] = True
    Mylist['ERRORTYPE'] = Timeout
except NameError:
    Mylist['ERROR'] = True
    Mylist['ERRORTYPE'] = NameError
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文