蟒蛇试试吧 [英] python try-finally

查看:146
本文介绍了蟒蛇试试吧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 foo 中的异常被忽略,但 bar 中的异常是否被提升?


Why does the exception in foo whizz by unnoticed, but the exception in bar is raised?

def foo():
    try:
        raise Exception('foo')
    finally:
        return

def bar():
    try:
        raise Exception('bar')
    finally:
        pass

foo()
bar()


推荐答案

p>从 Python文档


如果finally子句引发另一个异常或执行return或break语句,则保存的异常将丢失。

If the finally clause raises another exception or executes a return or break statement, the saved exception is lost.

这篇关于蟒蛇试试吧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆