Using Exceptions in a Simple Java Web Application with a Database

I have a question about exceptions in a medium sized Java web application. There is a data access layer implemented using JDBC with logic concentrated mainly in the servlet (UI - JSP). What is the regular exception hierarchy for such applications?

Should I catch exceptions at the data access level and reconstruct another exception for the whole (e.g. DataAccessException) or just let the highest level handle them (servlet).

In addition, I have a connection pool that is called at the data access level and has its own type of exceptions. Should these exceptions fall into the data access layer and be returned as a DataAccessException, or should they be handled by higher levels directly?

It would be nice to have an exception to the main application with two children: LogicException and TechnicalException. Will logic have subclasses like AuthentificationFailedException etc., while TechnicalExceptions will be responsible for passing error information such as data access level exception, FileNotFound (while it should be), etc.?

Thank!

+3
source share
4 answers

, , . , .

, , . , - :

public String getConfigurationProperty(String name) throws SQLException {
    // Try to read from my configuration table
}

,

public String getConfigurationProperty(String name) throws ConfigurationException {
    try {
        // Try to read from my configuration table 
    } catch (SQLException ex) {
        ConfigurationException wrapper = // Some subclass of ConfigurationException that wraps ex
        throw wrapper;
    }
}

. , , , , ,

public String getConfigurationProperty(String name) throws IOException {
    // Try to read from my configuration file
}

, IOException SQLException s. , , ConfigurationException . , , : , , .

, . , , , .

public String getConfigurationProperty(String name) throws ConfigurationException {
    try {
        // Try to read from my configuration file 
    } catch (IOException ex) {
        ConfigurationException wrapper = // Some subclass of ConfigurationException that wraps ex
        throw wrapper;
    }
}
+2

, , , , ( DataAccessException), . , , , , , . - , , : , " ", . .

, . - ... , , .:) " " "" , Q & A.:)

+2

, , . , SybaseException ( MVC-), DAO, .

:

  • apache + servlet 500.
  • . , .
  • RuntimeException , , .
  • , , . , , .

, , , RuntimeException, . , , , , catch-rethrow. , java , , . , , , .

+2

- . , , - , , , , .

.... . : . , ( ). , . - .

Not only exceptions, but also a business object must be shared. Preferred interfaces. This will allow you to change the implementation more easily, while your log processing code will depend on the same objects through your servlets for life.

+1
source

All Articles