简体   繁体   English

python try除了不能捕获所有错误

[英]python try except does not capture all errors

In my flask web application I use beaker library for session handling. 在我的flask Web应用程序中,我使用烧杯库进行会话处理。 In the following code, for some unknown reason, production server raises exception, but my local pc does just fine. 在下面的代码中,由于某种未知的原因,生产服务器会引发异常,但是我的本地PC可以正常运行。

import sys

...

try:
    beaker_session = request.environ['beaker.session']
    beaker_session['user_id'] = user.id 
    beaker_session.save()
except:
    flash(sys.exc_info()[0])
    return render_template('main/login.html')

Local computer saves the session just as expected, without any exception. 本地计算机按预期保存会话,没有任何例外。 Production server (RedHat OpenShift) raises an error exactly on "beaker_session.save()" line. 生产服务器(RedHat OpenShift)恰好在“ beaker_session.save()”行上引发错误。 But, instead of showing my login page with flash message, Internal Server Error 500 is thrown. 但是,没有显示带有闪存消息的登录页面,而是引发了内部服务器错误500。 I checked beaker backend url (mysql db) and there's no problem, because it works in other parts of code, where I persist newly registered users. 我检查了烧杯后端URL(mysql db),没有问题,因为它可以在代码的其他部分起作用,在这些部分中,我保留新注册的用户。 So, my question is 1) why except part doesn't work? 因此,我的问题是1)为什么除部分以外不起作用? 2) why beaker cannot save the session. 2)为什么烧杯无法保存会话。 Thank you. 谢谢。

By default Flask swallows exceptions, be sure to add this line to your application near the top: 默认情况下,Flask吞咽异常,请确保将此行添加到应用程序的顶部附近:

app.config['PROPAGATE_EXCEPTIONS'] = True app.config ['PROPAGATE_EXCEPTIONS'] =真

I'm not familiar with the beaker library, but if it's a SWIG wrapped library, and the exception ocurrs within the C++ code, it's possible the designer neglected to map the exception to an appropriate python exception. 我对烧杯库不熟悉,但是如果它是SWIG包装的库,并且C ++代码中发生了异常,则设计人员可能会忽略将异常映射到适当的python异常。 If this is the case, then Python does not get a shot at the exception -- and even try/except will miss it. 如果是这种情况,那么Python不会对异常产生任何影响-甚至try / except都会错过它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM