Skip to content
Advertisement

URL decoding: UnsupportedEncodingException in Java

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:

JavaScript

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.

Advertisement

Answer

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

JavaScript

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 …)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement