exception handling of Python
来源:互联网 发布:电工电子技术网络课 编辑:程序博客网 时间:2024/05/21 11:14
#exception handling of Python#how to do with the exception just like java#raising exceptions#multiple exceptions#geniric exception#ignoring exception how about pass#raising excpetiondef readFile(filename): if filename.endswith('txt'): fh = open(filename) return fh.readinto() else: raise ValueError('filename must be ended with txt')# ignoring exception how about passdef ignorexce(): counter = 0 try: fh=open("file.txt") # here can give an example of file not exists ,and then the finally block will get counter equal 1 ,but now it will be 0 print 'ignor' # csn not printlog except: pass # will not print anything of error log counter += 1 print "error" # print this error finally: # the finally block print "all work was done by Ethan" print ('calculate the error times %s ' % counter)def main(): try : fh = open("file.txt") x = 1/0 except IOError as e: print ('this my log :', e ) # if not found open file will print this log error in cousolut except ZeroDivisionError as e : print("u r divising by zero : ," ,e) except: # this is call generic exceptions ,this will catch everything ,this will it called generic print ("unkown reasons") else: print("this will not print any way") print "all done " # from here can know the excpetion will not break donw the process ,it will goes on try: for line in readFile("filename.tx1t") :print line # file not ended with txt will get raising exceptions except IOError as e1: print ('big eror :',e1) except ValueError as e : print ('this message from raising exceptions : ',e) ignorexce()main()
0 0
- exception handling of Python
- EXCEPTION HANDLING:A FALSE SENSE OF SECURITY
- EXCEPTION HANDLING:A FALSE SENSE OF SECURITY
- EXCEPTION HANDLING: A FALSE SENSE OF SECURITY
- JAVA fundamentals of exception handling mechanism
- Notes on Exception Handling in Python
- Exception handling
- Exception Handling
- Exception Handling
- Exception Handling
- Exception Handling
- Exception Handling
- exception handling
- Exception Handling
- Brief Intro to Exception Handling of Foundation Framework
- C# Exception Handling
- Exception Handling Rules
- Structured Exception Handling(SEH)
- Android Debug Database
- 常用页面布局技术
- Map按照Key排序和Map按照Value排序
- .net中的序列化
- QML创建一个带多个下拉输入框的窗口(ComboBox)
- exception handling of Python
- PHP内核探索之变量(5)- session的基本原理
- 解决:Dropzone.js的图片拖拽上传保存本地文件夹
- Eclipse CDT中出现 Nothing to build for XXX 的问题
- PHP 全局变量 局部变量 静态变量 常量
- 书写规则
- Android设计模式(十)-命令模式
- nyoj ACM:士兵杀敌(二)(树状数组)
- 遍历Map