Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why this code is not throwing IOException ?

 
Mahajan Bhupendra
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class Hello extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
PrintWriter out=response.getWriter();
out.println("<H1>Hello Freinds I m Fine</h1>");
out.flush();
out.close();
out.println("Hi i m also Here..");
}
}
after closing stream if i call
out.println("Hi i m also Here..");
shouldn't it throw IOException??
it's having no effect ..
why??
Bhupendra
 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahajan
yes calling write() on closed stream/writer *may* throw IOException
But in case of PrintWriter , print/println do not throw this exception , even if the writer is closed .
So Infact , given doGet() method never throws IOException .
try throwing this Exception explicitly, by adding at last line :
new throw IOException();
My knowledge is that for ServletException and its subclasses , behaviour of web-container is well-defined , as in spec .
But for rest of Exceptions ( including IOException ) , web-container may take its own course of action ( like wraaping it as ServletException , and propagating , or may be ignoring it as whole etc etc )
Tho i am not 100% sure about it . Plz correct me anyone if i am wrong

------------------
Gagan (/^_^\) SCJP2
Die-hard JavaMonk -- little Java a day , keeps u going .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic