美文网首页
python异常

python异常

作者: MagicalGuy | 来源:发表于2018-10-09 00:16 被阅读0次

    encoding:utf-8

    在Python中,所有的标识符都是一个标签

    f = print
    print('123456')
    type(print)
    print = 123
    f(type(print))
    def fun3( function ):
    function('123456')
    fun3(print)

    从异常基类派生出一个自定义异常类

    class NetConnectError(Exception):
    def init(self , errorMsg=''):
    NetConnectError.what= errorMsg

    def fun(name):
    if not isinstance(name,str):
    raise TypeError('类型错误')
    print('类型正确')

    print(isinstance(123 , str))
    try:
    fun(123)
    except Exception as err:
    print('fun产生异常:' , err)

    try:
    print(a)
    l1 = [1,2,3,4]
    l1[4] = 0
    except Exception as err: # 将IndexError异常对象保存到err中
    print('产生异常:',err)

    print('ssssss')

    ----------------

    try 和 finally

    def fun2():

    try:

    f = open('123.txt','w')

    if( 判断)

    return

    elif( 判断)

    return

    elif (判断)

    return

    finally:

    f.close()

    fun2()

    相关文章

      网友评论

          本文标题:python异常

          本文链接:https://www.haomeiwen.com/subject/lkplaftx.html