Stop at exception in my, not library code

后端 未结 5 2212
故里飘歌
故里飘歌 2021-02-14 22:33

I\'m developing an app using a Python library urllib and it is sometimes rising exceptions due to not being able to access an URL.

However, the exception is

5条回答
  •  臣服心动
    2021-02-14 23:07

    urllib can raise a lot of exceptions.

    You need to put a try block around the call into urllib and figure how to handle the exceptions for example:

    try:
        resp = urllib.request.urlopen(req)   
    
    except URLError as e:
        # analyse e to figure out the detail
        ...
    

    Certainly under python2's urllib lots of other exceptions are thrown. I'm not sure about python3's urllib.

提交回复
热议问题