[ 
https://issues.apache.org/jira/browse/HTTPCLIENT-904?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12799178#action_12799178
 ] 

Sebb commented on HTTPCLIENT-904:
---------------------------------

Removing a checked exception does affect consumers of the library.

If the caller currently catches UCE, they would have to amend their code if the 
method no longer throws UCE.

I just tried a test with the following code:

public class Test{
    private static void xys() //throws java.io.UnsupportedEncodingException 
    {        
    }
    private static void call(){
        try {
            xys();
        } catch (java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }        
    }
}

and I get:

Test.java:8: exception java.io.UnsupportedEncodingException is never thrown in 
body of corresponding try statement
        } catch (java.io.UnsupportedEncodingException e) {

It may not affect the code at run-time, but the user will have to amend their 
code before recompiling it.

> HttpMime StringBody constructor throws specification unnecessarily declares 
> UnsupportedEncodingException
> --------------------------------------------------------------------------------------------------------
>
>                 Key: HTTPCLIENT-904
>                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-904
>             Project: HttpComponents HttpClient
>          Issue Type: Bug
>          Components: HttpMime
>    Affects Versions: 4.0.1
>            Reporter: Mark Sinke
>
> The string body constructors that take a charset unnecessarily throw 
> UnsupportedEncodingException - if you have Charset, the encoding is by 
> definition supported:
>     public StringBody(
>             final String text, 
>             final String mimeType, 
>             Charset charset) throws UnsupportedEncodingException {
>         super(mimeType);
>         if (text == null) {
>             throw new IllegalArgumentException("Text may not be null");
>         }
>         if (charset == null) {
>             charset = Charset.defaultCharset();
>         }
>         this.content = text.getBytes(charset.name());
>         this.charset = charset;
>     }
>     
>     public StringBody(final String text, Charset charset) throws 
> UnsupportedEncodingException {
>         this(text, "text/plain", charset);
>     }
>     
> I suggest to change this to
>     public StringBody(
>             final String text, 
>             final String mimeType, 
>             Charset charset)  {
>         super(mimeType);
>         if (text == null) {
>             throw new IllegalArgumentException("Text may not be null");
>         }
>         if (charset == null) {
>             charset = Charset.defaultCharset();
>         }
>         this.content = text.getBytes(charset);
>         this.charset = charset;
>     }
>     
>     public StringBody(final String text, Charset charset) {
>         this(text, "text/plain", charset);
>     }
> The important change is to change
>         this.content = text.getBytes(charset.name());
> to 
>         this.content = text.getBytes(charset);
> which will not throw and hence the throws specifications can be removed.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to