translating SQLException to ServletException

Discussion in 'Java' started by Mohun Biswas, Jul 25, 2003.

  1. Mohun Biswas

    Mohun Biswas Guest

    This feels like something that would come up often but, not being a Java
    guru, I'm not sure of the solution. I have a web app with a number of
    servlets accessing a database via JDBC. I encapsulate all the JDBC work
    within a class called JDBCAccess. In order to allow code to be tested
    from the command line (or from other non-servlet-engine environments) I
    don't want JDBCAccess to use any javax.servlet.* classes. Thus my
    encapsulation is strict both ways: the servlets don't use any java.sql.*
    classes (directly) and the database code doesn't use any javax.servlet.*
    classes.

    This works fine with one irritating little problem: since each
    JDBCAccess method can throw a SQLException, each use of them within a
    servlet must do a boilerplate try/catch and re-throw it as
    ServletException, e.g.

    try {
    JDBC.doSomething();
    } catch (SQLException e) {
    throw new ServletException(e);
    }

    I can't declare my servlets to throw SQLException, partly because I
    don't want to and of course because the spec doesn't allow it anyway. Is
    there a simpler way? Can I maybe define my own exception class to
    "translate" from SQLException to ServletException, and if so what would
    that look like? Would it inherit from ServletException or SQLException?

    Thanks,
    MB
    Mohun Biswas, Jul 25, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pia
    Replies:
    0
    Views:
    518
  2. HARI OM
    Replies:
    0
    Views:
    9,346
    HARI OM
    May 4, 2005
  3. Replies:
    0
    Views:
    3,596
  4. Dix
    Replies:
    0
    Views:
    11,886
  5. googleRon

    ServletException vs packages

    googleRon, Feb 25, 2006, in forum: Java
    Replies:
    3
    Views:
    2,428
    googleRon
    Feb 26, 2006
Loading...

Share This Page