python自定义异常

发布时间:2019-09-16 07:34:33编辑:auto阅读(1588)

    python自定义异常


    try             异常在try块里抛,如果会产生多个异常,捕捉第一个,匹配except,后边的不再捕捉

    except: 抓异常

    else:       try无异常,才会执行else

    finally:   无论try块是否抛异常,永远执行的代码,通常用来执行关闭文件,断开服务器连接的功能

    [root@133 systeminformation]# vim ErrorExcept.py
    #!/usr/bin/env python                                                                                                                                                              
    #ecoding:utf-8                                                                                                                                                                     
                                                                                                                                                                                       
    class FuncError(Exception):                                                                                                                                                        
        def __str__(self):                                                                                                                                                             
            return "I am func Error"                                                                                                                                                   
                                                                                                                                                                                       
    def fun():                                                                                                                                                                         
        raise FuncError() #raise 抛出异常"I am func Error"                                                                                                                             
                                                                                                                                                                                       
    try:                                                                                                                                                                               
        fun()                                                                                                                                                                          
    except FuncError,e:                                                                                                                                                                
        print e                                                                                                                                                                        
                                                                                                                                                                                       
    print 'hello world'   
    
    [root@133 systeminformation]# python ErrorExcept.py 
    I am func Error
    hello world



    #!/usr/bin/env python                                                                                                                                                              
    #ecoding:utf-8                                                                                                                                                                     
    
    class FuncError(Exception):
        def __str__(self):
            return "I am func Error"
    
    def fun():
        raise FuncError() #raise 抛出异常"I am func Error"
    
    try:
       #fun()                                                                                                                                                           
        print  'a'         #print 'a'正确显示结果,如果是print a,报错name error,打印!!                                                                                                                                           
    except FuncError,e:    #如果print a 和fun()同时存在,print a在前,会打印!!,不打印I am fun error, fun()在前,打印I am fun error,不打印!!                                                                                                                                        
        print e                                                                                                                                                       
    except NameError:                                                                                                                                                 
        print '!!'                                                                                                                                                   
    else:                  #不抛异常,输出a,这种情况下执行else内容                                                                                                                                         
        print 'else'                                                                                                                                                  
    finally:               #finally无论如何都执行                                                                                                                                         
        print 'finally'
    print 'hello world'    #print一定会执行
    
    
    [root@133 systeminformation]# python ErrorExcept.py                                                                                                                                
    a
    else
    finally
    hello world
    
    
    #!/usr/bin/env python                                                                                                                                                              
    #ecoding:utf-8                                                                                                                                                                     
                                                                                                                                                                                       
    class FuncError(Exception):                                                                                                                                                        
        def __str__(self):                                                                                                                                                             
            return "I am func Error"                                                                                                                                                   
                                                                                                                                                                                       
    def fun():                                                                                                                                                                         
        raise FuncError() #raise 抛出异常"I am func Error"                                                                                                                             
                                                                                                                                                                                       
    try:                                                                                                                                                                               
       fun()               #即使有两个异常,抛出一个异常I am func Error,不再抛出第二个                                                                                                                                                                   
       print  a                                                                                                                                                                  
    except Exception:       #匹配所有异常,匹配即结束,打印all exception                                                                                                                                                          
        print 'all exception'                                                                                                                                                          
    except FuncError,e:                                                                                                                                                                
        print e                                                                                                                                                                        
    except NameError:                                                                                                                                                                  
        print '!!'                                                                                                                                                                     
    else:                   #没有异常菜执行else,有异常不执行                                                                                                                                                       
        print 'else'                                                                                                                                                                   
    finally:                                                                                                                                                                           
        print 'finally'                                                                                                                                                                
    print 'hello world'  
    
    [root@133 systeminformation]# python ErrorExcept.py 
    all exception
    finally
    hello world


    glob:python下类似shell中的*的通配符

    In [1]: import glob                                                                                                                                                                
                                                                                                                                                                                       
    In [2]: glob.glob('/etc/*.conf')    #匹配/etc下*.conf文件,保存到list中                                                                                                                                              
    Out[2]:                                                                                                                                                                            
    ['/etc/rsyslog.conf',
     '/etc/sensors3.conf',
     '/etc/GeoIP.conf',
     '/etc/Trolltech.conf',
     '/etc/nfsmount.conf',


    shlex :分词模块,

    [root@133 ~]# ps -eo pid,ppid,cmd                                                                                                                                            
      PID  PPID CMD
        1     0 /sbin/init
        2     0 [kthreadd]
        3     2 [migration/0]
        4     2 [ksoftirqd/0]
    
    [root@133 ~]# ipython
    In [1]: import shlex                                                                                                                                                               
                                                                                                                                                                                       
    In [2]: cmd = "ps -eo pid,ppid,cmd"                                                                                                                                                
                                                                                                                                                                                       
    In [3]: shlex.split(cmd)              #返回列表元素                                                                                                                                              
    Out[3]: ['ps', '-eo', 'pid,ppid,cmd']                                                                                                                                              
    
    In [4]: import subprocess                                                                                                                                                          
                                                                                                                                                                                       
    In [5]: subprocess.check_call(shlex.split(cmd))                                                                                                                                    
      PID  PPID CMD                                                                                                                                                                    
        1     0 /sbin/init                                                                                                                                                             
        2     0 [kthreadd]                                                                                                                                                             
        3     2 [migration/0]


关键字