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

  • Home
  • SEARCH
  • 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 266815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:28:31+00:00 2026-05-11T23:28:31+00:00

I noticed some confusion initially with my question. I’m not asking about how to

  • 0

I noticed some confusion initially with my question. I’m not asking about how to configure a logger nor how to use a logger properly, but rather how to capture all of the information that would have been logged at a lower logging level than what the current logging level is in the exception message.

I have been noticing two patterns in Java for logging information that may be useful to a developer when an exception occurs.

The following pattern seems very common. Basically, you just have your logger log information in-line as needed, so that when an exception occurs you have the log trace.

try {
    String myValue = someObject.getValue();
    logger.debug("Value: {}", myValue);
    doSomething(myValue);
}
catch (BadThingsHappenException bthe) {
    // consider this a RuntimeException wrapper class
    throw new UnhandledException(bthe);
}

The drawback with the above approach is that if your users require relatively quiet logs and need a high level of reliability to the point where they just can’t “try it again in debug mode”, the exception message contains insufficient data by itself to be useful to the developer.

The next pattern is one that I have seen that tries to mitigate this problem but seems ugly:

String myValue = null;
try {
    myValue = someObject.getValue();
    doSomething(myValue);
}
catch (BadThingsHappenException bthe) {
    String pattern = "An error occurred when setting value. [value={}]";
    // note that the format method below doesn't barf on nulls
    String detail = MessageFormatter.format(pattern, myValue);
    // consider this a RuntimeException wrapper class
    throw new UnhandledException(detail, bthe);
}

The above pattern seems to somewhat solve the problem, however, I’m not sure I like to declare so many variables outside the scope of the try block. Especially, when I have to deal with very complicated states.

The only other approach I have seen is using a Map to store key-value pairs that are then dumped into the exception message. I’m not sure I like that approach either since it seems to create code bloat.

Is there some Java voodoo out there that I am missing? How do you handle your exception state information?

  • 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-11T23:28:31+00:00Added an answer on May 11, 2026 at 11:28 pm

    We tend to create our most important application specific runtime exception classes with some special constructors, some constants and a ResourceBundle.

    Example snippet:

     public class MyException extends RuntimeException
     {
        private static final long serialVersionUID = 5224152764776895846L;
    
        private static final ResourceBundle MESSAGES;
        static
        {
            MESSAGES = ResourceBundle.getBundle("....MyExceptionMessages");
        }
    
        public static final String NO_CODE = "unknown";
        public static final String PROBLEMCODEONE = "problemCodeOne";
        public static final String PROBLEMCODETWO = "problemCodeTwo";
        // ... some more self-descriptive problem code constants
    
        private String errorCode = NO_CODE;
        private Object[] parameters = null;
    
        // Define some constructors
    
        public MyException(String errorCode)
        {
            super();
            this.errorCode = errorCode;
        }
    
        public MyException(String errorCode, Object[] parameters)   
        {
            this.errorCode = errorCode;
            this.parameters = parameters;
        }
    
        public MyException(String errorCode, Throwable cause)
        {
            super(cause);
            this.errorCode = errorCode;
        }
    
        public MyException(String errorCode, Object[] parameters, Throwable cause)
        {
            super(cause);
            this.errorCode = errorCode;
            this.parameters = parameters;
        }   
    
        @Override
        public String getLocalizedMessage()
        {
            if (NO_CODE.equals(errorCode))
            {
                return super.getLocalizedMessage();
            }
    
            String msg = MESSAGES.getString(errorCode); 
            if(parameters == null)
            {
                return msg;
            }
            return MessageFormat.format(msg, parameters);
        }
     }
    

    In the properties file we specify the exception descriptions, e.g.:

     problemCodeOne=Simple exception message
     problemCodeTwo=Parameterized exception message for {0} value
    

    Using this approach

    • We can use quite readable and understandable throw clauses (throw new MyException(MyException.PROBLEMCODETWO, new Object[] {parameter}, bthe))
    • The exception messages are “centralized”, can easily maintained and “internationalized”

    EDIT: change getMessage to getLocalizedMessage as Elijah suggested.

    EDIT2: Forgot to mention: this approach does not support Locale changing “on-the-fly” but it is intentional (it can be implemented if you need it).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed some posts here on string matching, which reminded me of an old
I noticed some code of a colleague today that initialized class variables in the
I noticed some people declare a private variable and then a public variable with
Recently, I noticed some people mentioning that std::list::size() has a linear complexity. According to
In VisualStudio (Pro 2008), I have just noticed some inconsistent behaviour and wondered if
I've noticed that some sites (usually banks) suppress the ability to paste text into
I have noticed that some apps like Safari and Mail show a loading indicator
I'm sure some of you noticed that if you have Acrobat Reader ( or
I noticed for a while now the following syntax in some of our code:
Hey! I was looking at this code at http://www.gnu.org/software/m68hc11/examples/primes_8c-source.html I noticed that in some

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.