I have a very simple servlet, which contains the following code to build a response:
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType(CONTENT_TYPE);
final PrintWriter out = response.getWriter();
// ...
My Sonar raises a critical issue with the rule: "Exceptions should not be thrown from servlet methods". Sonar explains it's a bad idea to let such exceptions be thrown:
Failure to catch exceptions in a servlet could leave a system in a vulnerable state, possibly resulting in denial-of-service attacks, or the exposure of sensitive information because when a servlet throws an exception, the servlet container typically sends debugging information back to the user. And that information could be very valuable to an attacker.
But if I understand their example, I cannot figure how to manage smartly the potential IOException on response.getWriter().
Some people can explain me when this statement can raise an exception, and how/why it's important to manage it by our-self?
EDIT: I accepted the first answer despite that I was a little frustrated. I understand very well it's a bad practice to let the servlet container manages this exception as the default behavior expose the stacktrace and possible other sensitive information to the world. In my case, the HTTP end-point was used for internal monitoring. So in my case, I wanted to expose (relevant) information and the question was HOW I can do that if I have no PrintWriter...
What I did: my program prints an error log and it returns an HTTP error code with no content. I don't know if it can really happen... but Sonar and me are happy.