pyhton---异常处理的终极语法、网页访问基本读取、网页访问异常处理
发布日期:2021-05-12 23:06:15 浏览次数:9 分类:精选文章

本文共 2926 字,大约阅读时间需要 9 分钟。

���������Python���������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������try-except������������������������������������������������

������������������

a = [1, 2, 3, 4, 5, 6]try:    print(a[6])except:    print("������������������")else:    print("hello!")finally:    print("xixi")
  • try���������������������������������������
  • except���������������������������������������
  • else���������try���������������������������������������������������
  • finally���������������������������������������������else������������������������������

������������������������������

import urlliburl = "http://www.baidu.com"try:    d = urllib.urlopen(url)    content = d.read()except:    print("������������������")else:    print(content)finally:    d.close()

���������������������try������������������������except������������������������������������������������������������else���������������������finally������������������������������

���������������������������

import urlliburl = "http://wasdasdasd"try:    d = urllib.urlopen(url)except IOError:    print("���������������������")except:    print("���������������������")else:    content = d.read()finally:    if d:        d.close()
  • IOError������������������������������URL������������
  • ������������������������������������������������������������������������������������
  • else������������������������
  • finally������������������������������������������������������

������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������

������1���������������������

try:    import urllib    d = urllib.urlopen("http://www.example.com")    content = d.read()    print(content)except:    print("���������������������������������")else:    print("���������������������������", content)finally:    if "Connection closed" in str(d):        d.close()

������2���������������������������

try:    with open("data.log", "r") as file:        print(file.read())except FileNotFoundError:    print("���������������")except PermissionError:    print("���������������������")except Exception as error:    print("������������������", str(error))

������������������������������������������������������������������������������������������������������������������������������������������

������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������try-except-else-finally������������������������������������������������������������

上一篇:python---异常处理
下一篇:linux----vi编辑器删除所有内容命令、ping网站指定数据包多少

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年04月27日 00时04分12秒