在 urllib.error 中使用 try catch 检查连接状态时出现另一个异常

Another exception occurred when Checking Connection status with a try catch in urllib.error

我试着做了一个简单的代码来检查电脑是否有互联网。 当电脑连接到互联网时,程序 运行 正确并打印“太棒了,谢谢你将我连接到互联网”但是当我 运行 程序没有互联网时,出现错误提示:During handling of the above exception, another exception occurred。 我错过了什么?

import urllib.request
import urllib.error

    loop_value = 1
    while (loop_value == 1):
        try:
            urllib.request.urlopen("https://www.google.com/")
        except urllib.error as e:
            print ("Run me again, after connecting")
        else:
            print ("Cool, thank you for connecting me to internet")
            loop_value = 0

错误-

Traceback (most recent call last):
  File "C:\Python\Python37\lib\urllib\request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "C:\Python\Python37\lib\http\client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Python\Python37\lib\http\client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Python\Python37\lib\http\client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Python\Python37\lib\http\client.py", line 1016, in _send_output
    self.send(msg)
  File "C:\Python\Python37\lib\http\client.py", line 956, in send
    self.connect()
  File "C:\Python\Python37\lib\http\client.py", line 1384, in connect
    super().connect()
  File "C:\Python\Python37\lib\http\client.py", line 928, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "C:\Python\Python37\lib\socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Python\Python37\lib\socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:/py/weathr.py", line 10, in <module>
    urllib.request.urlopen("https://www.google.com/")
  File "C:\Python\Python37\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python\Python37\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
  File "C:\Python\Python37\lib\urllib\request.py", line 543, in _open
    '_open', req)
  File "C:\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Python\Python37\lib\urllib\request.py", line 1360, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "C:\Python\Python37\lib\urllib\request.py", line 1319, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 11001] getaddrinfo failed>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:/py/weathr.py", line 12, in <module>
    except urllib.error as e:
TypeError: catching classes that do not inherit from BaseException is not allowed

你的错误意味着你试图捕获一个不是异常的对象(即 class 不是 BaseException 的实例)。只允许捕获异常,因为只能引发异常。

根据 docs,如果出现问题,方法 urllib.request.urlopen 会引发异常 URLError,因此请捕获它以便对错误做出反应。

示例实现:

try:
    urllib.request.urllopen("foo.com")
except urllib.error.URLError as e:
    print("Couldn't open foo.com. Error:")
    print(e)