4

(Referring to this post: How to throw an exception from an enum constructor?)

I would really like to do the same. Example Code:

public enum PublicIPWebservice {
    AMAZON_WEB_SERVICES("http://checkip.amazonaws.com"),
    EX_IP("http://api-ams01.exip.org/?call=ip"),
    WHAT_IS_MY_IP("http://automation.whatismyip.com/n09230945.asp");

private URL url;

private PublicIPWebservice(String url) throws MalformedURLException {
    this.url = new URL(url);
}

public URL getURL() {
    return url;
}
}

The program should crash if the url was not correct, as it would be a programming mistake, so catching the exception in the constructor would be wrong, wouldn't it?

What is the best way to solve that problem?

Community
  • 1
  • 1
S1lentSt0rm
  • 1,989
  • 2
  • 17
  • 28

5 Answers5

7

You can just catch it and rethrow as an AssertionError:

try {
    this.url = new URL(url);
}
catch(MalformedURLException e) {
    throw new AssertionError(e);
}
Matt Bryant
  • 4,841
  • 4
  • 31
  • 46
yshavit
  • 42,327
  • 7
  • 87
  • 124
2

I would just throw a RuntimeExpcetion of some kind (for example IllegalArgumentException)

private PublicIPWebservice(String url) {
    try {
        this.url = new URL(url);
    catch (MalformedURLException e) {
        throw new IllegalArgumentException(e);
    }
}
Aviram Segal
  • 10,962
  • 3
  • 39
  • 52
1

Usually a programmer error must be reported as an unchecked exception since it "will not occur" so theres no need to force yourself to catch the exception in client class.

That said you should wrap the url creation and throw an unchecked exception.

private PublicIPWebservice(String url) {
    try {
        this.url = new URL(url);
    catch (MalformedURLException ex) {
        throw new IllegalArgumentException("From input: " + url);
    }
}

Throwing an IllegalArgumentException is a good choice.

lpinto.eu
  • 2,077
  • 4
  • 21
  • 45
  • I like the rationale of using an unchecked exception. This cleans up most calling code, but lets something larger (pretend a plugin is throwing this exception in a build system like Jenkins) catch the exception, log it, and move on. – Josh Gagnon Jul 03 '13 at 19:13
0

Why wouldn't you want to catch that exception?

private PublicIPWebservice(String url) {
    try {
        this.url = new URL(url);
    catch (MalformedURLException e) {
        // surely you should handle the exception here
    }
}

What is the first user of this enum going to do with an exception. IT certainly wouldn't be expecting one.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    The point is that at runtime there is nothing one can do about a malformed String being entered. It'd be nice if it was a compile-time error, but `AssertionError` is probably the closest you can come to that. – Thorn G Dec 21 '12 at 19:29
  • 1
    You could create a unit test for this class that fails compilation for bad urls. That counts as a "compile time" check – Bohemian Dec 21 '12 at 19:31
0

If you really want the program to "crash" (terminate), System.exit(n) is (I think) the only absolute guarantee that nothing and nobody will handle some exception.

barneypitt
  • 979
  • 9
  • 11