繁体   English   中英

aws-lambda 函数中的异常处理

[英]Exception handling in aws-lambda functions

在尝试实现DNSRequest ,我还需要做一些异常处理并注意到一些奇怪的事情。 以下代码能够捕获 DNS 请求超时

def lambda_handler(event, context):

    hostname = "google.de"
    dnsIpAddresses = event['dnsIpAddresses']
    dnsResolver = dns.resolver.Resolver()
    dnsResolver.lifetime = 1.0
    result = {}

    for dnsIpAddress in dnsIpAddresses:
        dnsResolver.nameservers = [dnsIpAddress]

        try:
           myAnswers = dnsResolver.query(hostname, "A")
           print(myAnswers)
           result[dnsIpAddress] = "SUCCESS"
        except dns.resolver.Timeout:
           print("caught Timeout exception")
           result[dnsIpAddress] = "FAILURE"
        except dns.exception.DNSException:
           print("caught DNSException exception")
           result[dnsIpAddress] = "FAILURE"
        except:
           result[dnsIpAddress] = "FAILURE"
           print("caught general exception")

    return result

现在,如果我删除了 Timeout 块,并假设会发生超时,则在 DNSException 上,消息

捕获 DNSException 异常

永远不会显示。

现在,如果我删除了 DNSException 块,并假设会发生超时,消息

捕获一般异常

永远不会显示。

但是 Timeout 扩展了 DNSException,而 DNSException 扩展了 Exception。 我期望至少一般的期望块应该工作。

我错过了什么?

在您的最后一个块中,尝试使print行出现在result[dnsIpAddress] = "FAILURE"我的猜测是代码比此处显示的多,或者打印语句之前的行导致不同的异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM