当前位置:网站首页>[flask introduction series] exception handling

[flask introduction series] exception handling

2022-07-19 14:46:00 Hall owner a Niu

Personal profile

  • Author's brief introduction : Hello everyone , I'm Daniel , New star creator of the whole stack .
  • Blogger's personal website : A Niu's blog house
  • Stand by me : give the thumbs-up + Collection ️+ Leaving a message.
  • Series column :flask Framework quick start
  • Maxim : To be light , Because there are people who are afraid of the dark !
     Please add a picture description

Preface

Today is a brief introduction flask Methods for exception handling in , It belongs to the knowledge point that is less popular , Because we use python Knowledge to achieve these needs processing , But we still need to learn , Because he can help us deal with exceptions , Instead of exception handling in each view .

HTTP Exception active throw

  • abort Method
    Throw a given status code HTTPException Or specify the response , For example, you want to terminate a request with a page that does not find an exception , You can call abort(404)
  • Parameters
    code-HTTP Error status code of . Can only throw http Error code for protocol .
# Import Flask Classes and request object 
from flask import Flask,request,abort
app = Flask(__name__)


@app.route('/articles')
def get_articles():
    channel_id = request.args.get('channel_id')
    if channel_id is None:
        abort(400)   #400 Bad Request
    return 'you want get articles of channel {}'.format(channel_id)
# Flask Application's run Method start up web The server 
if __name__ == '__main__':
    app.run(port=8000)

 Insert picture description here

Capture the error

  • errorhandler Decorator
    Register an error handler , When the program throws the specified error status code , Will call the method decorated by the decorator .
  • Parameters
    code_or_exception - HTTP Error status code or specified exception .
  • For example, the unified processing status code is 500 The error of gives users a good prompt .
@app.errorhandler(500)
def internal_server_err(e):
    return " Server moved "
  • Catch the specified exception
@app.errorhandler(ZeroDivisionError)
def zero_division_err(e):
    return " Divisor cannot be zero "

example :

# Import Flask class 
from flask import Flask
app = Flask(__name__)

@app.errorhandler(ZeroDivisionError)
def zero_division_err(e):
    #  there e It's an exception object 
    print(e)
    return " Divisor cannot be zero "

@app.route("/")
def index():
    s = 1/0
    return s
# Flask Application's run Method start up web The server 
if __name__ == '__main__':
    app.run(port=8000)

 Insert picture description here
 Insert picture description here
As can be seen from the figure , If you encounter an exception, you will immediately stop executing the current view , Instead, execute the corresponding exception handling view .

Conclusion

If you think the blogger's writing is good , You can pay attention to the current column , Bloggers will finish this series ! You are also welcome to subscribe to other good columns of bloggers .

Series column
Soft grinding css
Hard bubble javascript
The front end is practical and small demo

原网站

版权声明
本文为[Hall owner a Niu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170637020841.html