zoukankan      html  css  js  c++  java
  • exception handling of Python

    #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 excpetion
    def 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 pass
    
    
    def 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()

  • 相关阅读:
    Apache ActiveMQ任意文件写入漏洞(CVE-2016-3088)复现
    Apache ActiveMQ序列化漏洞(CVE-2015-5254)复现
    Jenkins Java 反序列化远程执行代码漏洞(CVE-2017-1000353)
    jenkins未授权访问漏洞
    jenkins弱口令漏洞
    jboss 未授权访问漏洞复现
    jboss反序列化漏洞复现(CVE-2017-7504)
    C++中的强制类型转换
    new/delete与命名空间
    函数默认参数与占位参数
  • 原文地址:https://www.cnblogs.com/TendToBigData/p/10501254.html
Copyright © 2011-2022 走看看