• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why no error?

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good explanation Sathya
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yes, I remember now..
Must go back to the SCJP
Thanks,
Cyril.
 
reply
    Bookmark Topic Watch Topic
  • New Topic