-1

Help me guys. I have the following:

public class ParserException extends Exception {
    ParserException(ErrorMessage errorCode) {
        super(errorCode.getMessageText());
    }

    public enum ErrorMessage {
        DIVISION_BY_ZERO("Деление на ноль");

        private String messageText;

        ErrorMessage(String text) {
            this.messageText = text;
        }

        public String getMessageText() {
            return messageText;
        }
    }
}

I want to pass my enum instance name in the way like this, without need to use quotes:

throw new ParserException(DIVISION_BY_ZERO);

I don't like an idea to include something in the class where I throw mentioned exception. (Already have been suggested to use import static blah.blah.ParserException.ErrorMessage.*;)

Is there any other gentle solution to deal with that kind of things? As you suspect I'll be pleased to solve it simply by adding a couple lines into my ParserException constructor.

Any good ideas? Thx.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 1
    Why use the enum name's *String* as a parameter instead of the enum itself? The gain in type safety alone would make this worthwhile. **Edit:** Oh, I see, you are talking about passing in the enum itself and not the String. Your question is confusing. Please understand that variable names and Strings are not equivalent, not even close. The solution is as you state that you've already been told -- use a static import or use the fully qualified enum name. Why are you discounting those recommendations? – Hovercraft Full Of Eels Jun 14 '13 at 21:27
  • 1
    "I don't like an idea to include something in the class where I throw mentioned exception" <-- why? – fge Jun 14 '13 at 21:34
  • One of options is to make your inner enum class `static` and use it like `throw new ParserException(ParserException.ErrorMessage.DIVISION_BY_ZERO);` but since you don't want to add two quotation marks around `DIVISION_BY_ZERO` and make it String I doubt you will like this solution. Anyway it is just an option... – Pshemo Jun 14 '13 at 21:36
  • @Pshemo: correct me if I'm wrong, but I thought that inner enum types *are* static by default. **Edit**: as verified by none other than [Jon Skeet](http://stackoverflow.com/a/663849/522444). – Hovercraft Full Of Eels Jun 14 '13 at 21:40
  • 1
    @HovercraftFullOfEels I didn't get your comment right first time (damn, my Engilsh is bad...). You are absolutely right. By the way I didn't know that. Thanks for that portion of information :) – Pshemo Jun 14 '13 at 21:48

2 Answers2

2

Well, it is possible if you add in your imports:

import static complete.package.of.ErrorMessage.*;

This means that in code, ErrorMessage.FOO and FOO will be the same.

Note: Java 5+, but nobody uses Java 4- today, right? :p

Note 2: you can also override .toString() in an enum, just make it return messageText

fge
  • 119,121
  • 33
  • 254
  • 329
0

Due to the awkward syntax required to use inner enum instances, I would make your enum a top-level class (in its own java file) If you do that, using it becomes clean and simple.

Although I prefer avoiding class bloat, in such cases I allow it, because everything is simpler, which is good.

And who knows... someday you may want to use this class elsewhere.

Bohemian
  • 412,405
  • 93
  • 575
  • 722