python be_careful_with_finally.py

Posted

tags:

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

# coding=UTF-8

def lost_exception():
    while True:
        try:
            print("hello world")
            raise IndexError("rrr")
        except NameError as e:
            print 'NameError happened'
            print e
        finally:
            # IndexError is saved for reraise
            # but it won't reraise if
            # 1. new excepton happens in finally
            # 2. return
            # 3. break
            print("finally executed")
            break

def false_return(n):
    try:
        if n <= 0:
            raise ValueError("n must > 0")
        else:
            # return in finally gets executed first
            return n
    except ValueError as e:
        print("ValueError")
        print e
    finally:
        print("in finally")
        return -1

def main():
    # lost_exception()
    print(false_return(-100))
    print(false_return(10))


if __name__ == "__main__":
    main()

以上是关于python be_careful_with_finally.py的主要内容,如果未能解决你的问题,请参考以下文章

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python

python初识