Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Error

 
samart mateo
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I'm relatively new to Servlet...need some favor in getting my first servlet going thru...


ok, below's part of my servlet coding:



My UserService class would throws these exceptions:

ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException.

Unfortunately, when i tried building the class, it gave me this errors:

Error(15,17): method doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) in class riskmanagement.user.LoginCheck cannot override method doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) in class javax.servlet.http.HttpServlet, overridden method does not throw class java.lang.ClassNotFoundException.


"Error(15,17): method doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) in class riskmanagement.user.LoginCheck cannot override method doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) in class javax.servlet.http.HttpServlet, overridden method does not throw class java.lang.InstantiationException"


"Error(15,17): method doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) in class riskmanagement.user.LoginCheck cannot override method doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) in class javax.servlet.http.HttpServlet, overridden method does not throw class java.lang.IllegalAccessException"


"Error(15,17): method doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) in class riskmanagement.user.LoginCheck cannot override method doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) in class javax.servlet.http.HttpServlet, overridden method does not throw class java.sql.SQLException"



Can someone please help me?? how to solve this and get my servlet running...

Thank you very much for your help!!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by samart mateo:

public void doPost(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException,
ClassNotFoundException, InstantiationException,
IllegalAccessException, SQLException {


}

[/CODE]





remove below exceptions from your method declaration .it is violating the overriding rules
--------------------------------------------------------------
ClassNotFoundException, InstantiationException,
IllegalAccessException, SQLException
----------------------------------------------------------------

and i think, it is not the Exception. it will give you compilation error

Hope This helps
 
samart mateo
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i removed the exception, it will return error such as below:

Error(34,43): unreported exception: class java.lang.ClassNotFoundException; must be caught or declared to be thrown

Error(34,43): unreported exception: class java.lang.InstantiationException; must be caught or declared to be thrown

Error(34,43): unreported exception: class java.lang.IllegalAccessException; must be caught or declared to be thrown


It blows my mind as to what i should do exactly...huhu
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by samart mateo:
if i removed the exception, it will return error such as below:



It blows my mind as to what i should do exactly...huhu


Then i do not know.. but try this one ...let see

it may be the problem with your methods which are used by your servlet... so better put your code inside try then specify common Exception// but your mentioning only SQLException insteadof you put Exception
 
samart mateo
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but your mentioning only SQLException insteadof you put Exception


This doesnt really matter, even if i code every single Exceptions to be thrown, it still give the same error. I think it should have something to do with missing some other declaration...
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Exception in catch clause and see what



this code throwing !
 
Matteo Di Furia
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't let the servlet code launch exceptions that are not declared in the HttpServlet signs. In this case, the servlet must throw only ServletException and IOException, nothing else.
If your code contains statements which can cause other types of exceptions, you must catch them and handle them in the proper way.
Commonly, on exception like for example SQLException, what you have to do is to catch it, generate some sort of error message and redirect/forward to an error jsp which will display the error message (note that this way the doPost method is not throwing the root exception, is handling it).
More generally, exceptions caused by your methods should be handled directly by your own code (error message generation, error logging or whatever you want), the servlet should throw ServletException or IOException only if something went wrong in the request/response process.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64971
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The proper way to deal with exceptions in a servlet:

You wrap the XyzException (which stands for any non-servlet exception) in a ServletException and throw it. This way, the original exception becomes the root cause of the servlet exception, and any error handling mechanisms established in the deployment descriptor take effect.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64971
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Matteo Di Furia:
Commonly, on exception like for example SQLException, what you have to do is to catch it, generate some sort of error message and redirect/forward to an error jsp which will display the error message
It is much better to wrap the exception as I have shown in the previous reply and to let the error handlers declared in the deployment descriptor take effect. Handling the exceptions in servlet code is not recommended.

the servlet should throw ServletException or IOException only if something went wrong in the request/response process.
No. ServletException should be used to wrap any exceptions that occur (except for IOException which can safely be thrown on its own).
[ August 01, 2008: Message edited by: Bear Bibeault ]
 
samart mateo
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
thank you so much for your advices...now i know how to handle exceptions in servlet...what i did was



dat'll be just fine
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64971
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by samart mateo:
dat'll be just fine

Please use real words when posting to the forums. Abbreviations such as "dat" in place of "that" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

And so, you think it's "fine" for an exception to get invisibly eaten while just writing out the stack trace to a log that no one will read?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic