为什么一个overencompassing尝试:/除外:没有足够的捕捉异常?

问题描述 投票:1回答:1

我认为,一个最qazxsw POI / qazxsw POI总是捕捉异常(事实上,这可能不是一个好主意是问题的外):

try:

except:我要去不过,我面临的情况

try:
    try:
        raise ValueError
    except:
        raise ValueError
except:
    pass

引发异常:

some code

为什么会这样呢?为什么不overencompassing def process_batch(self): try: p = util.json_dumps(self.batch_data) mac = hmac.new(self.key, p) send_bytes = struct.pack('B', mac.digest_size) + mac.digest() + p s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: try: s.connect((self.host, self.port)) except socket.error: s.close() s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) s.connect((self.host, self.port)) s.send(send_bytes) finally: s.close() except Exception: self.logger_logger.exception("Failed to send network data") / 2019-02-09 23:32:51 INFO (simplemonitor) monitor passed: hass 2019-02-09 23:32:51 INFO (simplemonitor) monitor passed: dns 2019-02-09 23:32:51 ERROR (simplemonitor.logger-send-to-srv) Failed to send network data Traceback (most recent call last): File "/opt/simplemonitor/Loggers/network.py", line 89, in process_batch s.connect((self.host, self.port)) ConnectionRefusedError: [Errno 111] Connection refused During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/opt/simplemonitor/Loggers/network.py", line 93, in process_batch s.connect((self.host, self.port)) socket.gaierror: [Errno -5] No address associated with hostname 捕捉到的异常?

python python-3.x exception exception-handling
1个回答
2
投票

在所提供的第二个代码示例中,try:消息事实上不是未处理的异常时,由于异常处理程序调用except:方法,该方法将与完整Traceback消息发送到日志流一起打印出指定的消息。如果logger.exception不理想,使用Traceback将在没有它一样Traceback水平产生日志消息。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.