使用urllib.error时发生错误-在处理上述异常期间,发生了另一个异常:

时间:2018-10-08 08:29:11

标签: python-3.x urllib traceback

我试图编写一个简单的代码来检查个人计算机是否可以连接互联网。 当PC连接到Internet时,程序会正确运行并显示“很酷,谢谢您将我连接到Internet”。但是当我在没有Internet的情况下运行程序时,会出现错误。我想念什么?

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

1 个答案:

答案 0 :(得分:0)

您的错误意味着您正在尝试捕获不是异常的对象(即,不是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)