java 什么时候可以从 Servlet 抛出 ServletException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/839225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When is it ok to throw a ServletException from a Servlet?
提问by Raymond Roestenburg
I have been throwing ServletExceptions in the past, when something/anything goes wrong in a Servlet, mostly just wrapping the exception in ServletException.
我过去一直在抛出ServletExceptions,当 Servlet 中出现某些问题/任何错误时,主要是将异常包装在ServletException.
Now I am thinking it is actually better to not throw a ServletExceptionbut to respond with response.sendError(sc)and use the correct HTTP status codes.
现在我认为最好不要抛出 aServletException但响应response.sendError(sc)并使用正确的 HTTP 状态代码。
If I can't send an error using reponse.sendError, (IOException), I wrap the IOExceptionin a ServletException.
如果我无法通过发送错误reponse.sendError,( IOException),我包裹IOException在一个ServletException。
Is the above a better way to respond?
When is it OK to just throw a ServletException?
以上是更好的回应方式吗?什么时候可以只扔一个ServletException?
回答by pharsicle
I have just come to the opposite conclusion to @alamar. My situation is writing a REST-style servlet to be called by code in an Oracle database, not by humans.
我刚刚得出与@alamar 相反的结论。我的情况是编写一个 REST 风格的 servlet,由 Oracle 数据库中的代码调用,而不是由人类调用。
I want to return the HTTP code 400 Bad Requestto the caller when the request information is invalid or missing. Throwing a ServletExceptioncauses the container to return 500 Internal Server Error, which states there is something wrong with the server, not with the request.
当请求信息无效或丢失时,我想将 HTTP 代码400 Bad Request返回给调用方。抛出 aServletException会导致容器返回500 Internal Server Error,这表明服务器有问题,而不是请求有问题。
Here is the solution I adopted.
这是我采用的解决方案。
- Create a simple exception class
RequestExceptionthat extendsException. - Check the validity of the request with method(s) that throw
new RequestException(message). Catch
RequestExceptionin the servlet'sdoPostmethod and callHttpServletResponse.sendError()like this:try { validateRequest(request); // Do stuff with a valid request. } catch (RequestException e) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); }
- 创建一个简单的异常类
RequestException,扩展Exception. - 使用 throw 的方法检查请求的有效性
new RequestException(message)。 捕获
RequestExceptionservlet 的doPost方法并HttpServletResponse.sendError()像这样调用:try { validateRequest(request); // Do stuff with a valid request. } catch (RequestException e) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); }
The message is returned to the caller as it would be with a ServletException.
消息将返回给调用者,就像带有ServletException.
回答by alamar
It is better to throw a ServletException.
最好扔一个ServletException.
You can later actually catch this exception with error-page directive in your web.xml, and make appropriate actions: display a fancy error page to user, send the exception along with the stack trace to developers, write it to logs and so on.
您稍后可以在您的 中使用 error-page 指令实际捕获此异常web.xml,并执行适当的操作:向用户显示一个花哨的错误页面,将异常与堆栈跟踪一起发送给开发人员,将其写入日志等等。
If you just use sendError(), you can't do that, to various extents.
如果你只是使用sendError(),你不能在不同程度上做到这一点。

