gpt4 book ai didi

python - flask 如何在使用@ app.errorhandler(Exception)时不显示错误页面,但继续进行应用程序流程

转载 作者:行者123 更新时间:2023-12-03 08:40:00 24 4
gpt4 key购买 nike

我有

@app.errorhandler(Exception)
def unhandled(error):
print(error)
etype, value, tb = sys.exc_info()
print(traceback.print_exception(etype, value, tb))
logger.error("Exception %s" % traceback.format_exc())
logger.error("Exception %s" % traceback.print_exception(etype, value, tb))
logger.info("Exception %s" % traceback.format_exc())
logger.info("Exception %s" % traceback.print_exception(etype, value, tb))
print(traceback.format_exc())
return None

@app.route('/')
def index():
logger.info("A %s- B: %s" % project_dict) # raise exception
a = 1
b = 2
c = 3
问题是我没有这个函数来中断调用处理程序@ app.route('/')的流程
只是打印以记录并继续
a = 1
b = 2
c = 3

最佳答案

您如何将可能的错误执行集成到try-except块中?

# This code is never executed due to an error that occurs within 
# the try-except block in the route `index`.
@app.errorhandler(Exception)
def unhandled(error):
return make_response('Internal Server Error', 400)

@app.route('/')
def index():
try:
# Your code that may throw an error.
raise Exception('something went wrong') # raise exception
except Exception as exc:
# Handle exception here!
pass
else:
# This code is executes if no exception occurs.
pass
finally:
# This code is executed despite an exception.
pass
# ...

关于python - flask 如何在使用@ app.errorhandler(Exception)时不显示错误页面,但继续进行应用程序流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62489516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com