为了人工抛出一个异常,可以使用断言assert,如下:
#coding=utf-8
def testAssert(path):
import os
assert isinstance(path,str),'Need string'
if os.path.isdir(path) == True:
print path + ' is a dir!'
return path
else:
return "Not dir!"
if __name__ == '__main__':
print testAssert(2)
测试如下:
D:\work\bjmobile\daily_report>python test.py
Traceback (most recent call last):
File "test.py", line 13, in
print testAssert(2)
File "test.py", line 5, in testAssert
assert isinstance(path,str),'Need string'
AssertionError: Need string
#coding=utf-8
def testAssert(path):
import os
assert isinstance(path,str),'Need string'
if os.path.isdir(path) == True:
print path + ' is a dir!'
return path
else:
return "Not dir!"
if __name__ == '__main__':
print testAssert(2)
测试如下:
D:\work\bjmobile\daily_report>python test.py
Traceback (most recent call last):
File "test.py", line 13, in
print testAssert(2)
File "test.py", line 5, in testAssert
assert isinstance(path,str),'Need string'
AssertionError: Need string
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10972173/viewspace-2125003/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10972173/viewspace-2125003/