Forums Register Login

why no error?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Good explanation Sathya
+Pie Number of slices to send: Send
Oh yes, I remember now..
Must go back to the SCJP
Thanks,
Cyril.
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 801 times.
Similar Threads
Displaying using constructor
unable to compile servlet
Code Review - ChatServlet
Beginners ques- servlet not deploying
unable t o compile the servlet .. plz help
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 03:28:18.