Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:11:35+00:00 2026-05-26T14:11:35+00:00

I am working on a largish Java web application with Spring, Hibernate, and some

  • 0

I am working on a largish Java web application with Spring, Hibernate, and some other libraries, including Apache log4j for logging. One of my ongoing projects is to rewrite a large number of exceptions in the legacy areas (which I didn’t write!) of the code to give more sensible information. A typical exception block looks like this:

try {
  //Some Hibernate business here
}
catch (Exception e) {   //Yes, Exception. That's not just me being general. I find this especially frustrating.
  log4j.error("Fail to XXXXXX");   //again, real
  throw new MyException();
}

As you can guess, this makes for some challenging logs. I’m looking for a standard way to get better info out of these exceptions. Most of them wrap Hibernate calls, if that helps. Here’s a typical block similar to one I just wrote:

try {
    myList.add( ((myClass) commonService.getRecordByTableId(myClass.class, ID)).toString() );
} catch (ServiceException e) {
    log4j.error("Failed to retrieve record from table myClass for id " + ID);
    e.printStackTrace();
}

Here I’m pulling a record from the database and adding it to a list. In the catch block I log what I think is a sensible message about what the try block is doing, and print a stack trace. So, my question: is there anything else I could/should be doing, in general, to get better information for diagnosing errors?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:11:36+00:00Added an answer on May 26, 2026 at 2:11 pm

    In exceptions handling, Logging and re-throwing is a popular anti-pattern. You shouldn’t do that. You need to decide between catching the exception, then properly handling it (including logging), or not catching it and allowing it to pass to higher levels (or rethrow it / wrap it in a runtime exception if it was a checked exception).

    If you do that (log + rethrow), then there’s no way for the upstream code to know that you’ve already logged the exception, and so the same exception might get logged twice or more, depending how many layers the exception has to go through, and what layer arbitrarily decide to log and re-throw it. That will make reading the logs and undestanding them a complete nightmare.

    Also you could argue that throwing and catching exceptions are costly operations, all this catching and rethrowing isn’t helping your performance at runtime.

    So if you choose to actually handle the exception, then swallowing it is not the right way (even if you log some message). At the very least, you need to log the trace:

    log4j.error("Failed to retrieve record from table myClass for id " + ID, e);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working on some legacy hibernate code. How do I do the following with hbm.xml(hibernate
working on internationalizating a web application, I am a bit blocked, because I don't
Working with some basic java apps on CentOS 5 linux and I have my
Working with a website (as opposed to a web application) in VS2010 so there
Working with python interactively, it's sometimes necessary to display a result which is some
Working in Eclipse on a Dynamic Web Project (using Tomcat (v5.5) as the app
Working with an Oracle 9i database from an ASP.NET 2.0 (VB) application using OLEDB.
I am working on a largish C# project with a lot of Windows Forms
Working on some matrix code, I'm concerned of performance issues. here's how it works
Working on an iPhone application through a TightVNC connection into a Mac Mini; the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.