提交 44d2101d 编写于 作者: 鞋子不会飞's avatar 鞋子不会飞

http错误进行异常捕获, 以及主动抛出http错误,一般根据情况在前方拦截处理

上级 5d2f6a75
...@@ -213,6 +213,13 @@ def demo6(): ...@@ -213,6 +213,13 @@ def demo6():
return "demo6", 700, {"b": 20} return "demo6", 700, {"b": 20}
############################################################可以捕获 http错误 和 系统内置错误#######################################################################
@app.errorhandler(404) # 可以捕获 http错误 和 系统内置错误
def error_404(e): # 必须定义形参来接收具体的错误信息
return "您访问的页面去浪迹天涯了... %s" % e
# Flask应用程序实例的run方法启动WEB服务器 # Flask应用程序实例的run方法启动WEB服务器
if __name__ == '__main__': if __name__ == '__main__':
print(app.url_map) print(app.url_map)
......
...@@ -211,20 +211,14 @@ def login_use_session(): ...@@ -211,20 +211,14 @@ def login_use_session():
# flask中对http错误封装了异常处理, 可以对http错误进行异常捕获, 以及主动抛出http错误 # flask中对http错误封装了异常处理, 可以对http错误进行异常捕获, 以及主动抛出http错误
@user_blu.errorhandler(404) # 可以捕获 http错误 和 系统内置错误
def error_404(e): # 必须定义形参来接收具体的错误信息
return "您访问的页面去浪迹天涯了... %s" % e
@user_blu.errorhandler(ZeroDivisionError) @user_blu.errorhandler(ZeroDivisionError)
def error_zero(e): def error_zero(e):
return '除数不能为0' return '除数不能为0'
@user_blu.route('/') @user_blu.route('/index_error')
def index_error(): def index_error():
# a = 1 / 0 a = 1 / 0
abort(404) # 主动抛出http错误 abort(404) # 主动抛出http错误
return "index" return "index"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册