
本文共 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������������������������������������������������������������
发表评论
最新留言
关于作者
