일반적인 예외를 잡는 것이 파이썬에서 합리적입니까? isinstance()
을 사용하여 적절하게 처리하기 위해 특정 유형의 예외를 감지합니까?Python에서 isinstance를 사용하여 특정 유형의 예외를 확인하는 것이 합리적입니까?
나는 시간 제한, NXDOMAIN 응답 등과 같은 것들을 예외로하는 dnspython 툴킷을 가지고 놀고있다. 이러한 예외는 dns.exception.DNSException
의 서브 클래스이므로, 합리적인지 궁금하다. 또는 pythonic, DNSException
을 잡은 다음 isinstance()
으로 특정 예외를 확인하십시오.
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입니다.
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"
이 조항의 순서에 대한주의 : 첫 번째 일치하는 절을 이동합니다, 그래서 마지막에 슈퍼 클래스에 대한 검사를 이동하기위한 여러 except
조항은 무엇
감사 스벤 ... 그게 훨씬 낫 네요. – Vortura