Python-Flask-抛出异常

from flask import Flask
from flask import abort

app = Flask(__name__)

@app.route('/')
def index():
return 'index'

@app.route('/demo1')
def demo1():
abort(404)
return 'demo1'

@app.route('/demo2')
def demo2():
a = 0
b = 1 / a
return "demo2"

#使用装饰器的形式去捕获指定的错误码和异常
@app.errorhandler(404)
def page_not_found(error):
return "页面不见了"

@app.errorhandler(ZeroDivisionError)
def zero_division_error(error):
return "除数不能为零"

if __name__ == '__main__':
app.run(debug=True)

posted @ 2020-07-13 15:23  墨笔冰花  阅读(297)  评论(0编辑  收藏  举报