1. IntroductionThis article mainly talks about URLError and HTTPError, as well as some processing methods. 2. URLError1. Explain the three possible reasons for URLError:
2. Case examples:In the code, try-except statements are needed to surround and capture corresponding exceptions.
The urlopen method was used to access a non-existent URL. Running results: Note: It shows that the error code is 11004 and the error reason is getaddrinfo failed. HTTPErrorHTTPError is a subclass of URLError. When a request is made using the urlopen method, the server will have a corresponding response object, which contains a digital "status code". example: The caught exception is HTTPError, which has a code attribute, which is the error code, and a reason attribute, which is an attribute of its parent class URLError.
Running results: 1. Code analysisThe error code is 403 and the error reason is Forbidden, which means the server prohibits access. As we know, the parent class of HTTPError is URLError. According to programming experience, the exception of the parent class should be written after the child class exception. If the child class cannot catch it, then the parent class exception can be caught. 2. Optimize the code
If HTTPError is caught, the code is output and the URLError exception is not processed. If a non-HTTPError occurs, the URLError exception is caught and the cause of the error is output. In addition, you can add the hasattr attribute to judge the attribute in advance. The code is rewritten as follows
3. Exception handling methodFirst, judge the abnormal attributes to avoid attribute output errors. If the response is a "redirect", you need to locate another address to get the document, urllib2 will handle this. Note: When an HTTPError instance is generated, it will have a code attribute, which is the relevant error number sent by the server. Because urllib2 can handle redirects, that is, codes starting with 3 can be processed, and numbers in the range of 100-299 indicate success, so only error numbers 400-599 are seen. IV. ConclusionThis article is based on the basics, through case analysis and code display. It solves the problem of handling URLError null exceptions in practical applications. It introduces two main exception errors and provides solutions to the corresponding errors. Everyone is welcome to try actively. Sometimes it is easy to implement something when you see others do it, but when you try to implement it yourself, there will always be various problems. Don't be too ambitious and be diligent in doing it, so that you can understand it more deeply. The use of language can make readers understand the content of the article more clearly and intuitively. The code is very simple, I hope it will be helpful for learning. |
<<: Facebook launches new AI project to learn from videos
This article is reprinted from the WeChat public ...
An open network port is the simplest access point...
Last year, Intel and Broadcom performed the first...
spinservers has sent the latest 2023 US Labor Day...
AsiaYun is a domestic business founded in 2018. I...
Hosteons also launched a Double 11 event, offerin...
Is there a data cable? My seat is in the first ro...
CUBECLOUD has launched a promotional activity cal...
On December 10, 2021, the "2021 (11th) Telec...
[51CTO.com original article] In February 2017, th...
On September 6, during HUAWEI CONNECT 2017, Huawe...
DesiVPS has released the latest promotion in July...
Residents are excited about and choosing to live ...
At present, 5G, as a global emerging strategic in...