首页 > 代码库 > python学习笔记7-异常处理
python学习笔记7-异常处理
1 写弄成了读
1 try: 2 fh = open("testfile", "r") 3 fh.write("This is my test file for exception handling!!") 4 except IOError: 5 print ("Error: can\‘t find file or read data") 6 7 else: 8 print ("Written content in the file successfully") 9 Error: can‘t find file or read data
2 异常触发
1 #异常触发 2 def functionName( level ): 3 if level < 1: 4 raise "Invalid level!", level 5 # The code below to this would not be executed 6 # if we raise the exception
3 自定义异常
1 #自定义异常 2 class Networkerror(RuntimeError): 3 def __init__(self, arg): 4 self.args = arg 5 6 try: 7 raise Networkerror("Bad hostname") 8 except Networkerror,e: 9 print e.args
python学习笔记7-异常处理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。