Home > Common Problem > Inventory of URLError and HTTPError exception handling methods

Inventory of URLError and HTTPError exception handling methods

Release: 2023-07-25 16:23:26
forward
4464 people have browsed it

1. Preface

This article mainly talks about URLError and HTTPError, as well as some processing methods.


2. URLError

#1. Explain what URLError may occur 3 reasons:

# 1. 网络无连接,即本机无法上网。


# 2. 连接不到特定的服务器。


# 3. 服务器不存在。
Copy after login

2. Case

Example:

In the code, try-except statements need to be used to surround and catch the corresponding exceptions.

# coding:UTF8


import urllib.request


request = urllib.request.urlopen("http://www.baidu.com")


try:
    urllib.request.urlopen(request)
    print("[Errno 11004] getaddrinfo failed")
except urllib.URLError as e:
    print(e.reason)
Copy after login

Used the urlopen method to access a non-existent URL.

Run result:

Inventory of URLError and HTTPError exception handling methods

## Note:

It shows that the error code is 11004 and the cause of the error is getaddrinfo failed.


三、HTTPError

HTTPError 是 URLError 的子类,在利用 urlopen 方法发出一个请求时,服务器上都会对应一个应答对象 response,其中它包含一个数字”状态码”。

例:

捕获的异常是 HTTPError,它会带有一个 code 属性,就是错误代号,另外又打印了 reason 属性,这是它的父类 URLError 的属性。

import urllib2
req = urllib2.Request('http://blog.csdn.net/cqcre')
try:
    urllib2.urlopen(req)
except urllib2.HTTPError, e:
    print e.code
    print e.reason
Copy after login

运行结果:

Inventory of URLError and HTTPError exception handling methods

1. 代码解析

错误代号是 403,错误原因是 Forbidden,说明服务器禁止访问。

知道,HTTPError 的父类是 URLError,根据编程经验,父类的异常应当写到子类异常的后面,如果子类捕获不到,那么可以捕获父类的异常,

2. 优化代码

import urllib2
req = urllib2.Request('http://blog.csdn.net/cqcre')
try:
    urllib2.urlopen(req)
except urllib2.HTTPError, e:
    print e.code
except urllib2.URLError, e:
    print e.reason
else:
    print "OK"
Copy after login

Inventory of URLError and HTTPError exception handling methods

如果捕获到了 HTTPError,则输出 code,不会再处理 URLError 异常。如果发生的不是HTTPError,则会去捕获 URLError 异常,输出错误原因。

另外还可以加入 hasattr 属性提前对属性进行判断,代码改写如下

import urllib2
req = urllib2.Request('http://blog.csdn.net/cqcre')
try:
    urllib2.urlopen(req)
except urllib2.URLError, e:
    if hasattr(e,"code"):
        print e.code
    if hasattr(e,"reason"):
        print e.reason
else:
    print "OK"
Copy after login

Inventory of URLError and HTTPError exception handling methods

3. 异常处理方法

  1. 首先对异常的属性进行判断,以免出现属性输出报错的现象

  2. 假如 response 是一个”重定向”,需定位到别的地址获取文档,urllib2 将对此进行处理。

注 :

HTTPError 实例产生后会有一个 code 属性,这就是是服务器发送的相关错误号。

因为 urllib2 可以为处理重定向,也就是 3 开头的代号可以被处理,并且 100-299 范围的号码指示成功,所以只能看到 400-599 的错误号码。


四、总结

本文基于基础,通过案例的分析,代码的展示。解决在实际应用中,对于URLError空异常的处理方式。介绍了两种主要的异常错误。以及提供了相应错误的解决方案处理方法。

The above is the detailed content of Inventory of URLError and HTTPError exception handling methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
PHP parsing error: syntax error, unexpected '
From 1970-01-01 08:00:00
0
0
0
Report an error
From 1970-01-01 08:00:00
0
0
0
Error when uploading file format error
From 1970-01-01 08:00:00
0
0
0
html5 - EventSource error
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template