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 909811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:56:35+00:00 2026-05-15T16:56:35+00:00

I have a series of exception messages: enum myEnum { BASIC(1, An error occurred),

  • 0

I have a series of exception messages:

enum myEnum {
    BASIC(1, "An error occurred"),
    WITH_ERRORID(2, "Error id: {0}"),
    DETAILED(3, "Problem in record {0} with index {1}");
};

I also have a single, reusable authority for logging and throwing a custom exception:

public void throwException (myEnum message) {
    log.addEntry(message);
    throw new CustomException(message);
}

Calling the method is straight forward:

throwException (myEnum.DETAILED);

I am now wrestling with the most elegant way to format the strings. I could add a toString() method to the enum to format the string based on the number of inputs, and change throwException to accept a String instead:

String msg = message.toString(variable);
throwException (msg);

String msg2 = message.toString(variable, otherVariable);
throwException (msg2);

String msg3 = message.toString(variable, otherVariable, nextVariable);
throwException (msg3);

While this would work, I would like to move the the repeated toString calls to within throwException(). I was considering passing an ArrayList to throwException(). But I would then have to check the size of the list before I format the string:

if (list.size == 1) MessageFormat.format(message, list.get(0));
if (list.size == 2) MessageFormat.format(message, list.get(0), list.get(1));

Is a better technical or design approach for solving this problem?

  • 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-15T16:56:36+00:00Added an answer on May 15, 2026 at 4:56 pm

    Since you’re using the enum type declaration, I’m going to assume you’re using JSE 5 or later.

    Given that, I’d say you’re most of the way to a solution. Here’s my version of your enum:

    public enum myEnum {
        BASIC(1, "An error occurred"),
        WITH_ERRORID(2, "Error id: {0}"),
        DETAILED(3, "Problem in record {0} with index {1}");
        private int key = 0;
        private String format = null;
    
        private myEnum(int aKey, String aFormat) {
            this.key=aKey;
            this.format=aFormat;
        }
    
        /**
         * This will take whatever input you provide and use the enum instance format
         * string to generate a message.
         */
        public String getMessage(Object... someContents) {
            return MessageFormat.format(this.format, someContents);
        }
    }
    

    To use these modifications, you need to make one minor change to your throwException() implementation:

    public void throwException (myEnum message, String... errorContents) {
        String formattedMessage = message.getMessage(errorContents);
        log.addEntry(formattedMessage);
        throw new CustomException(formattedMessage);
    }
    

    Using the varargs notation means that you can pass zero or more values of the same type, and they’re treated as an array in the called code. No arguments? It’s treated as a zero-length array, and no formatting occurs.

    The only difficulty remaining is that I can’t see any obvious way to alert the developer at compile-time that more arguments are required to fill out the formatting. If you pass no error details with a myEnum.DETAILED, you get back the format you started with.

    You should already have the strings needed to inject into your formats; now you just need to pass them to the throwException() method. How does this look as an option?

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

Sidebar

Ask A Question

Stats

  • Questions 473k
  • Answers 473k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Besides scalability, SOA offers architectural flexibility. If you decide at… May 16, 2026 at 3:53 am
  • Editorial Team
    Editorial Team added an answer You have built the ffmpeg binary which can run on… May 16, 2026 at 3:53 am
  • Editorial Team
    Editorial Team added an answer WinDbg + SOS can help understand that. But it takes… May 16, 2026 at 3:53 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.