#!/usr/bin/python
#-*- coding:utf8 -*-
#简单的文件异常
try:
file("Jack.txt", "w+")
print "write file"
except IOError:
print "the file can't be written"
except:
print "except"
#比较常见的除法异常
try:
ret = 10 / 0
except ZeroDivisionError:
print "0 is not allowed"
except:
print "except"
try:
string = "jack"
try:
print string[1]-string[2]
except TypeError:
print "can't do it"
except:
print "except"
#但是有时候 文件打开了 但是在读写的时候出现问题
#该怎么办
try:
f = open("Jack.txt", "r")
try:
print fsock.read(5)
except:
print "file read error"
finally:
f.close()
except IOError:
print "file can't find"
#raise
try:
string = None
if string in None:
print "string is a none number"
raise NameError
print len(string) #空对象有长度?
except TypeError:
print "the none doesn't have length"
#assert 断言
ss = "Jack"
assert(len(ss) > 3) #正确
sss = "Smith"
#assert(len(sss) == 1) #会引发断言错误
#由于python在网络方面使用较多
#下面介绍下网络方面的简单异常
#功能: 发送GET, 然后获取信息 filename中表示信息的路径
import sys, socket
host = '' #填入你需要的网址
port = 6000
filename = '\\' #表示根路径
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, e:
print "create sock error, error is %s" %e
sys.exit(1)
try:
port = int(port)
except ValueError:
try:
port = socket.getservbyname(port, "tcp")
except socket.error, e:
print "port is error"
sys.exit(1)
try:
sock.connect((host, port))
except socket.gaierror, e:
print "connect error %s" %e
sys.exit(1)
try:
sock.sendall("GET %s HTTP/1.0\r\n\r\n" % filename)
except socket.error, e:
print "send data error %s" %e
while True:
try:
buf = sock.recv(1028) #获取文件信息
except socket.error, r:
print "error in recving data %s" %e
sys.exit(1)
if not len(buf):
break
sys.stdout.write(buf)
#-*- coding:utf8 -*-
#简单的文件异常
try:
file("Jack.txt", "w+")
print "write file"
except IOError:
print "the file can't be written"
except:
print "except"
#比较常见的除法异常
try:
ret = 10 / 0
except ZeroDivisionError:
print "0 is not allowed"
except:
print "except"
try:
string = "jack"
try:
print string[1]-string[2]
except TypeError:
print "can't do it"
except:
print "except"
#但是有时候 文件打开了 但是在读写的时候出现问题
#该怎么办
try:
f = open("Jack.txt", "r")
try:
print fsock.read(5)
except:
print "file read error"
finally:
f.close()
except IOError:
print "file can't find"
#raise
try:
string = None
if string in None:
print "string is a none number"
raise NameError
print len(string) #空对象有长度?
except TypeError:
print "the none doesn't have length"
#assert 断言
ss = "Jack"
assert(len(ss) > 3) #正确
sss = "Smith"
#assert(len(sss) == 1) #会引发断言错误
#由于python在网络方面使用较多
#下面介绍下网络方面的简单异常
#功能: 发送GET, 然后获取信息 filename中表示信息的路径
import sys, socket
host = '' #填入你需要的网址
port = 6000
filename = '\\' #表示根路径
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, e:
print "create sock error, error is %s" %e
sys.exit(1)
try:
port = int(port)
except ValueError:
try:
port = socket.getservbyname(port, "tcp")
except socket.error, e:
print "port is error"
sys.exit(1)
try:
sock.connect((host, port))
except socket.gaierror, e:
print "connect error %s" %e
sys.exit(1)
try:
sock.sendall("GET %s HTTP/1.0\r\n\r\n" % filename)
except socket.error, e:
print "send data error %s" %e
while True:
try:
buf = sock.recv(1028) #获取文件信息
except socket.error, r:
print "error in recving data %s" %e
sys.exit(1)
if not len(buf):
break
sys.stdout.write(buf)