| Author |
why no error?
|
cyril vidal
Ranch Hand
Joined: Jul 02, 2003
Posts: 247
|
|
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.
|
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
|
 |
Sathya Sankar
Ranch Hand
Joined: Sep 16, 2000
Posts: 67
|
|
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
|
SCJP, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
|
 |
Andres Gonzalez
Ranch Hand
Joined: Nov 27, 2001
Posts: 1561
|
|
Good explanation Sathya
|
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
|
 |
cyril vidal
Ranch Hand
Joined: Jul 02, 2003
Posts: 247
|
|
Oh yes, I remember now.. Must go back to the SCJP Thanks, Cyril.
|
 |
 |
|
|
subject: why no error?
|
|
|