aspose file tools*
The moose likes Java in General and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
Hi All,

I am developing a project whose architecture is something like this :

JSP --> Servlet --> Bean(which interactes with DB)

JSP <-- Servlet <-- Bean(which interactes with DB)

Now ,I am trying to catch any exception and display using error page.

Structure of my Bean :

public Vector getDump(String strTblName, String strColNames, String strConditions) throws SQLException, Exception
{
try
{
-- does something
}
catch (SQLException sqle)
{
throw new SQLException(sqle.getMessage()+" -> Error in taking dump");
}
catch(Exception e)
{
throw new Exception(e.getMessage()+" -> Error in Extraction");
}

Now in My servlet I am trying to rethrow the exception so as to catch itis errorPage.

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
if (task.equals("takeDump"))
{
try
{
vecDump = dbMan.getDump(strTblName,strColNames,strConditions);
---- do some work here
}
catch(IOException ioe)
{
throw new IOException(ioe.getMessage()+" -> Error in IO");
}
catch (SQLException sqle)
{
throw new SQLException(sqle.getMessage()+" -> Error in SQL ");
}
}
}
But in the End I am getting error as :

unreported exception java.lang.Exception; must be caught or declared to be thrown :=>> throw new Exception(sqle.getMessage()+" -> Error in SQL");

Even if I change SQLException to Exception, I am getting the same sort of error.


Please help !!!

Thanks in advance.
Nilesh
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
the service methods in a Servlet can throw ServletException and IOException, not Exception or SQLException.

You shouldn't do things like that anyway, rather use a correct mechanism to handle the exception and pass a decent error to the JSP for display.


42
Mudi Appu
Ranch Hand

Joined: Apr 06, 2005
Posts: 44
Because of you have thrown java.lang.Exception the getDump() method of your bean you should catch it or Throw it everywhere you call that method.

catch in withinn yur doPost()
[ April 18, 2005: Message edited by: kanishtha Kramalekhakha ]

SCJP 2
Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
Hi,
Thanks for the reply.

Even if I catch Exception in doPost(), I am still getting the same error.

And can you please tell me what can I do coz this is the way I have designed my project.

I want to throw this SQLEception from servlet, and catch it in errorPage(jsp).

Regards,
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
You CAN'T throw SQLException from a service method. You have IOException and ServletException and that's IT.

And if you throw either, you'll never even get to your JSP, but you'll get your server's errorpage for HTTP status 500.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I want to throw this SQLEception from servlet, and catch it in errorPage(jsp).

You can't throw a SQLException from doPost - for the reason Jeroen has already pointed out. But you can throw ServletException or IOException. I'd recommend you log the actual SQLException and rethrown a ServletException with a more user friendly message (after all why would you want to show users SQLExceptions?).

[Ah, Jeroen beat me to the reply]
[ April 07, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
wood burning stoves
 
subject: Exceptions