python 自定义异常类学习

简介:

#自定义异常类
class MyException(Exception):
    pass

def exextp_01():
    try:
        #come code here
        list = ['java','python','php']
        raise MyException  #通过异常名字抓取自定义异常 
    except:
        print 'no thie is variabe'
        print 'MyException encoutered!'

exextp_01()

def except_test():
    for i in range(1,100):
        try:
            print  "Number %s"%i
            time.sleep(0.2)
        except KeyboardInterrupt:
            print 'Please do not interupt me ,I am doing the important tash here!'
            continue

===================
try    finally 
学习

def exception_03():
    try:
       raise MyExepion,"and some addition add is not exception"
    except MyExepion,data:
        print "MyException encoutered!"
        print data

exception_03()

def exception_02():
    try:
        raise MyExepion
    except MyExepion:
        print "MyException encoutered"
    finally:
        print "Arrive finally"

def exeption_01():
    try:
        print "normal code here" \
              ", this is here"
    except MyExepion:
        print "MyEception encoutered"
    else:
        #No exceptions will execute the else
        print "No exception"  #You can test your module

#exeption_01()









本文转自 知止内明 51CTO博客,原文链接:http://blog.51cto.com/357712148/2046965,如需转载请自行联系原作者
目录
相关文章
|
2天前
|
运维 Shell Python
Shell和Python学习教程总结
Shell和Python学习教程总结
|
2天前
|
Python
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
|
2天前
|
开发框架 前端开发 数据库
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
|
2天前
|
数据采集 数据可视化 数据处理
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
|
2天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python
20 0
|
3天前
|
Python
Python 一步一步教你用pyglet制作可播放音乐的扬声器类
Python 一步一步教你用pyglet制作可播放音乐的扬声器类
14 0
|
9天前
|
机器学习/深度学习 算法 Python
使用Python实现集成学习算法:Bagging与Boosting
使用Python实现集成学习算法:Bagging与Boosting
19 0
|
10天前
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
52 0
|
10天前
|
索引 Python
python 格式化、set类型和class类基础知识练习(上)
python 格式化、set类型和class类基础知识练习
33 0