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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:09:20+00:00 2026-05-11T02:09:20+00:00

I am writing a web service in Java, and I am trying to figure

  • 0

I am writing a web service in Java, and I am trying to figure out the best way to define error codes and their associated error strings. I need to have a numerical error code and an error string grouped together. Both the error code and error string will be sent to the client accessing the web service. For example, when a SQLException occurs, I might want to do the following:

// Example: errorCode = 1,  //          errorString = 'There was a problem accessing the database.' throw new SomeWebServiceException(errorCode, errorString); 

The client program might be shown the message:

‘Error #1 has occured: There was a problem accessing the database.’

My first thought was to used an Enum of the error codes and override the toString methods to return the error strings. Here is what I came up with:

public enum Errors {   DATABASE {     @Override     public String toString() {       return 'A database error has occured.';     }   },    DUPLICATE_USER {     @Override     public String toString() {       return 'This user already exists.';     }   },    // more errors follow } 

My question is: Is there a better way to do this? I would prefer an solution in code, rather than reading from an external file. I am using Javadoc for this project, and being able to document the error codes in-line and have them automatically update in the documentation would be helpful.

  • 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. 2026-05-11T02:09:21+00:00Added an answer on May 11, 2026 at 2:09 am

    Well there’s certainly a better implementation of the enum solution (which is generally quite nice):

    public enum Error {   DATABASE(0, "A database error has occurred."),   DUPLICATE_USER(1, "This user already exists.");    private final int code;   private final String description;    private Error(int code, String description) {     this.code = code;     this.description = description;   }    public String getDescription() {      return description;   }    public int getCode() {      return code;   }    @Override   public String toString() {     return code + ": " + description;   } } 

    You may want to override toString() to just return the description instead – not sure. Anyway, the main point is that you don’t need to override separately for each error code. Also note that I’ve explicitly specified the code instead of using the ordinal value – this makes it easier to change the order and add/remove errors later.

    Don’t forget that this isn’t internationalised at all – but unless your web service client sends you a locale description, you can’t easily internationalise it yourself anyway. At least they’ll have the error code to use for i18n at the client side…

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Eric Lippert posted about strong signing assemblies: The purpose of… May 12, 2026 at 12:30 pm
  • Editorial Team
    Editorial Team added an answer It can be a good idea, as part of a… May 12, 2026 at 12:30 pm
  • Editorial Team
    Editorial Team added an answer Yes. Any object put into a collection is sent a… May 12, 2026 at 12:30 pm

Related Questions

I am writing a C# client that calls a web service written in Java
I am writing a Java web service for legacy SCO machines (which only have
I joined a new company about a month ago. The company is rather small
I've inherited a Java web-services code-base (BEA/Oracle Weblogic) and need to start/launch an external

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.