Flask errorhandler isn't called for view that returns 404
<p>With the following functions, I'd expect the error handler to get called when the view returns 404. But it does not trigger. Why doesn't a 404 error handler get called when a view returns 404?</p>

<pre><code>@app.errorhandler(404)
def error(e):
    return render_template('error.html'), 404


@app.route('/error/')
def error_test():
    return '', 404
</code></pre>

<p>Error handlers handle errors. If you just <code>return '',404</code> then your function has finished normally, it just returned an 404 HTTP code. There were no errors, so error handler is not called.</p>

<p>In python we report errors with exceptions. If something goes wrong, you throw it. Flask has special base exception class <a href="http://werkzeug.pocoo.org/docs/0.11/exceptions/#werkzeug.exceptions.HTTPException" rel="nofollow">HTTPException</a>. If you throw it, then appropriate error handler would be used to render it. See <a href="http://werkzeug.pocoo.org/docs/0.11/exceptions/#werkzeug.exceptions.HTTPException" rel="nofollow">HTTPException documentation</a> for examples. There is also a bunch of predefined exceptions and in your case you need to throw <a href="http://werkzeug.pocoo.org/docs/0.11/exceptions/#werkzeug.exceptions.NotFound" rel="nofollow">NotFound</a>.</p>

python flask