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.
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 ]