• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why no error?

 
cyril vidal
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
I wonder why the following code compiles and runs well, although the signature of the doGet method is not correct, since the ServletException declaration is missing:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CompteurServlet extends HttpServlet
{
int nb_visites = 0;
public void doGet(HttpServletRequest requete, HttpServletResponse reponse) throws IOException
{
PrintWriter pw = reponse.getWriter();
nb_visites++;
pw.println("<html>");
pw.println("<head>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h3>Vous avez acc�d� � cette serlvet " + nb_visites + " fois.</h3>");
pw.println("<body>");
pw.println("</html>");
}
}
Don't we have to declare ServletException in the throws clause? It seems to be a checked exception, so we should, no?
Regards,
Cyril.
 
Sathya Sankar
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cyril,
This is an example of polymorphism - method overriding. In Java when a subclass over-rides a superclass' method, the restriction on the throws clause is: The child method (in the sub class) cannot throw any "extra" exception that is not a child of any of the exceptions thrown by the parent method.
Thus, the exceptions in the child method should :
1. Either be in the list of exceptions thrown by the parent method
2. Or be a sub class of any of the exceptions listed in the parent method
3. Java allows child methods to throw a subset of the exceptions thrown by the parent method.
Ciao,
GSS
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good explanation Sathya
 
cyril vidal
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes, I remember now..
Must go back to the SCJP
Thanks,
Cyril.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic