54

What I understand from the documentation is that UnsupportedEncodingException can only be thrown if I specify a wrong encoding as the second parameter to URLDecoder.decode(String, String) method. Is it so? I need to know cases where this exception can be thrown.

Basically, I have this code segment in one of my functions:

if (keyVal.length == 2) {
    try {
        value = URLDecoder.decode(
            keyVal[1],
            "UTF-8");
    } catch (UnsupportedEncodingException e) {
          // Will it ever be thrown?
    }
}

Since I am explicitly mentioning "UTF-8", is there any way this exception can be thrown? Do I need to do anything in the catch block? Or, if my understanding is completely wrong, please let me know.

4 Answers 4

56

It cannot happen, unless there is something fundamentally broken in your JVM. But I think you should write this as:

try {
    value = URLDecoder.decode(keyVal[1], "UTF-8");
} catch (UnsupportedEncodingException e) {
    throw new AssertionError("UTF-8 is unknown");
    // or 'throw new AssertionError("Impossible things are happening today. " +
    //                              "Consider buying a lottery ticket!!");'
}

The cost of doing this is a few bytes of code that will "never" be executed, and one String literal that will never be used. That a small price for the protecting against the possibility that you may have misread / misunderstood the javadocs (you haven't in this case ...) or that the specs might change (they won't in this case ...)

Sign up to request clarification or add additional context in comments.

5 Comments

What are some practical likely examples that a JVM will be broken?
@Pacerier - I cannot think of any. You'd probably need to modify the "rt.jar" file (or worse) to break this. So it is theoretically possible ... but not something you are likely to ever encounter unless you are creating / using an experimental JVM.
Is there any reason why this excpetion is checked?
Presumably because the designers thought that this was an error condition that a typical application should handle. (In hindsight, that was probably a mistake, but it can't be fixed without breaking binary compatibility.)
The other issue is that it is only "UTF-8" where there is a guarantee that the exception cannot be thrown. For other encoding names ... the exception is possible, and making the exception checked is a reasonable decision. Maybe the solution would be to add a URLDecoder.decodeUTF8( method that didn't throw the exception at all.
16

That's because of the odd choice to make UnsupportedEncodingException checked. No, it won't be thrown.

I usually do as follows:

} catch (UnsupportedEncodingException e) {
  throw new AssertionError("UTF-8 not supported");
}

Comments

6

In your special case - no, it won't be thrown. Unless you execute your code in a Java runtime that does not support "UTF-8".

6 Comments

Such a runtime is not supposed to exist - at least in JDK 1.6, UTF-8 is a standard charset. download.oracle.com/javase/6/docs/api/java/nio/charset/…
@Mat - I believe this too but .. Oracle is not the only vendor of Java runtimes. The java language spec does not mention any encodings that have to be supported (except: UTF-16 which is the specified encoding for char)
Wow, thanks. I thought it did, and that javadoc seems to imply it but you're correct, that's not in the JLS.
@Andreas_D: While the JLS does not specify encodings, the javadocs linked by Mat are a specification as well, and are normative. So any certified Java implementions must support UTF-8 in the provided implementation of the Java core API.
javadocs are normative? Do you have a reference for that statement? Honestly, I do not believe that one has to implement the JLS and all of the javaDoc details (of which version, btw?) in order to get a certificate.
|
5

To answer an old question for newer readers:

Java 11 now has URLDecoder.decode(String, Charset); which does not throw. So you don't have to use a try-catch block at all. Just do:

URLDecoder.decode(keyVal[1], StandardCharsets.UTF_8);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.