• 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

Exceptions

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Nilesh Srivastava
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic