文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

python自定义异常

2023-01-31 06:43

关注

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]


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯