python自定义异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python自定义异常相关的知识,希望对你有一定的参考价值。

python自定义异常


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

except: 抓异常

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

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

[[email protected] 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‘   

[[email protected] 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一定会执行


[[email protected] 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‘  

[[email protected] 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 :分词模块,

[[email protected] ~]# ps -eo pid,ppid,cmd                                                                                                                                            
  PID  PPID CMD
    1     0 /sbin/init
    2     0 [kthreadd]
    3     2 [migration/0]
    4     2 [ksoftirqd/0]

[[email protected] ~]# 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]


本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1880468

以上是关于python自定义异常的主要内容,如果未能解决你的问题,请参考以下文章

python自定义异常

自定义异常中的默认消息 - Python

VSCode自定义代码片段——CSS选择器

片段中的自定义列表适配器

在python 3.6中处理自定义编码时遇到类型错误

python自定义异常抛出接受多个数值