aspose file tools*
The moose likes Servlets and the fly likes Exception Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Exception Error" Watch "Exception Error" New topic
Author

Exception Error

samart mateo
Ranch Hand

Joined: Feb 06, 2006
Posts: 37
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Feb 06, 2006
Posts: 37
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Feb 06, 2006
Posts: 37
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

Use Exception in catch clause and see what



this code throwing !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Matteo Di Furia
Ranch Hand

Joined: Jun 20, 2008
Posts: 102
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

Joined: Jan 10, 2002
Posts: 61424
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

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

Joined: Feb 06, 2006
Posts: 37
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

Joined: Jan 10, 2002
Posts: 61424
    
  67

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Error