Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm a bit clueless on how to handle exceptions. What I've think so far is to use a custom error page for the users so they can't see the full exception for security reasons and log the exception in a .txt file.

So this is how my DAO methods structure is:

try {
        //stuff
    } 
    catch (SQLException e) {
        exceptionHandler(e);
        throw new RuntimeException(e);

    } 
    finally {
        if (conn != null) {
            try {
                conn.close();
            } 
            catch (SQLException e) 
            {}
        }
    }

And this is the method that handles all the exceptions:

public void exceptionHandler(SQLException e){
    Logger logger = Logger.getAnonymousLogger();
    logger.log(Level.SEVERE, "an exception was thrown", e);
    try {
        FileOutputStream fos;
        fos = new FileOutputStream(new File("userDAOexceptions.txt"), true);
        PrintStream ps = new PrintStream(fos);  
        e.printStackTrace(ps);
        fos.close();
        ps.close();
    } catch (FileNotFoundException e1) {} 
      catch (IOException e1) {}  
}

Is this considered a good way to handle exceptions in the DAO layer?

share|improve this question
    
You're building a web application with spring-mvc? If so, you can take advantage of Spring exception handler. The second problem is you shouldn't write logs into a file manually. You should add a FileAppender into your logger so that you can eliminate the large chunk of logging code. – Hung Tran Dec 17 '15 at 12:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.