我的编程空间,编程开发者的网络收藏夹
学习永远不晚

python自定义异常

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

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

python自定义异常

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

python自定义异常

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

python自定义异常

python自定义异常try             异常在try块里抛,如果会产生多个异常,捕捉第一个,匹配except,后边的不再捕捉except: 抓异常else:       try无异常,才会执行elsefinally:   无论
2023-01-31

python 自定义异常类学习

#自定义异常类class MyException(Exception): passdef exextp_01(): try: #come code here list = ['java','pytho
2023-01-31

浅谈python抛出异常、自定义异常, 传递异常

一. 抛出异常 Python用异常对象(exception object)表示异常情况,遇到错误后,会引发异常。如果异常对象并未被处理或捕捉,程序就会用所谓的回溯(Traceback,一种错误信息)终止执行。 raise 语句 Python
2022-06-04

python自定义异常和主动抛出异常(r

有时候python自带异常不够用,如同java,python也可以自定义异常,并且可以手动抛出。注意,自定义异常只能由自己抛出。python解释器是不知道用户自定义异常是什么鬼的。主动抛出异常。 格式: 主动抛出异常终止程序 raise 异
2023-01-31

python用户怎么自定义异常

本篇内容主要讲解“python用户怎么自定义异常”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“python用户怎么自定义异常”吧!说明1、程序可以通过创建一个新的异常类来命名它们自己的异常。异常
2023-06-20

python怎么实现自定义异常类

本篇内容介绍了“python怎么实现自定义异常类”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!说明通常可以继承Exception或子类。命名
2023-06-30

python自定义异常是什么意思

本篇内容介绍了“python自定义异常是什么意思”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!说明1、因为错误是class,捕获错误是捕获c
2023-06-20

怎么在python中自定义异常类

这篇文章将为大家详细讲解有关怎么在python中自定义异常类,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。python是什么意思Python是一种跨平台的、具有解释性、编译性、互动性和面向对
2023-06-14

java自定义错误异常

java自定义错误异常下面代码定义了一个异常解决除法的问题,如果除数出现0或负数就报错注意,自定义异常的代码第一次运行时一定要写在应用的前面,否则会出现错误无法加载主类java.lang.NoClassDefFoundError下面程序复制即可到java上运行
java自定义错误异常
2015-12-23

JavaScript如何自定义异常

这篇文章给大家分享的是有关JavaScript如何自定义异常的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1.概念1.1什么是错误与异常所谓的错误就是编程的过程中使程序不能正常运行的状态,也称为异常。在JavaS
2023-06-21

Java异常怎么自定义

这篇文章主要介绍“Java异常怎么自定义”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Java异常怎么自定义”文章能帮助大家解决问题。异常方法//返回此可抛出对象的详细信息消息字符串public S
2023-07-05

python中如何使用自定义异常类

本篇文章为大家展示了python中如何使用自定义异常类,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。自定义异常类1.自定义类2.继承系统的异常基类exception3.自定义异常类的构造函数等方法进
2023-06-20

Java异常学习之自定义异常详解

前言哎呀,妈呀,又出异常了!俗话说:“代码虐我千百遍,我待代码如初恋”。小Alan最近一直在忙着工作,已经很久没有写写东西来加深自己的理解了,今天来跟大家聊聊Java异常。Java异常的体系什么的,理论知识啥的我就懒得去BB太多了,是个搞J
2023-05-31

PHP异常处理:自定义异常类详解

php 中的自定义异常类允许开发者创建特定于应用程序的异常类型,添加额外的信息和处理逻辑。通过继承自 exception 类,自定义异常类可以包含属性(如错误代码)和方法(如获取错误详细信息)。这增强了 php 的异常处理机制,提供了更灵活
PHP异常处理:自定义异常类详解
2024-05-14

编程热搜

  • Python 学习之路 - Python
    一、安装Python34Windows在Python官网(https://www.python.org/downloads/)下载安装包并安装。Python的默认安装路径是:C:\Python34配置环境变量:【右键计算机】--》【属性】-
    Python 学习之路 - Python
  • chatgpt的中文全称是什么
    chatgpt的中文全称是生成型预训练变换模型。ChatGPT是什么ChatGPT是美国人工智能研究实验室OpenAI开发的一种全新聊天机器人模型,它能够通过学习和理解人类的语言来进行对话,还能根据聊天的上下文进行互动,并协助人类完成一系列
    chatgpt的中文全称是什么
  • C/C++中extern函数使用详解
  • C/C++可变参数的使用
    可变参数的使用方法远远不止以下几种,不过在C,C++中使用可变参数时要小心,在使用printf()等函数时传入的参数个数一定不能比前面的格式化字符串中的’%’符号个数少,否则会产生访问越界,运气不好的话还会导致程序崩溃
    C/C++可变参数的使用
  • css样式文件该放在哪里
  • php中数组下标必须是连续的吗
  • Python 3 教程
    Python 3 教程 Python 的 3.0 版本,常被称为 Python 3000,或简称 Py3k。相对于 Python 的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python 3.0 在设计的时候没有考虑向下兼容。 Python
    Python 3 教程
  • Python pip包管理
    一、前言    在Python中, 安装第三方模块是通过 setuptools 这个工具完成的。 Python有两个封装了 setuptools的包管理工具: easy_install  和  pip , 目前官方推荐使用 pip。    
    Python pip包管理
  • ubuntu如何重新编译内核
  • 改善Java代码之慎用java动态编译

目录