问题出在这个字符串构造函数上 (JAVA)

huangapple go评论75阅读模式
英文:

Problem with this String constructor (JAVA)

问题

I am trying to use the constructor

public String(byte[] bytes,
              Charset charset)

Which is detailed here.
I'm using it to convert an array of bytes to ASCII. Here is the code

String msg = new String(raw, "US-ASCII");

Unfortunately, this gives me:

error: unreported exception UnsupportedEncodingException; must be caught or declared to be thrown
    String msg = new String(raw, "US-ASCII");
                 ^

Trying a different configuration like "String msg = new String(data, 0, data.length, "ASCII");" Doesn't work either.

Is this no longer a usable constructor, or am I doing something wrong?

英文:

I am trying to use the constructor

public String(byte[] bytes,
              Charset charset)

Which is detailed here.
I'm using it to convert an array of bytes to ASCII. Here is the code

String msg = new String(raw, "US-ASCII");

Unfortunately, this gives me:

error: unreported exception UnsupportedEncodingException; must be caught or declared to be thrown
    String msg = new String(raw, "US-ASCII");
                 ^

Trying a different configuration like "String msg = new String(data, 0, data.length, "ASCII");" Dosen't work either.

Is this no longer a usable constructor, or am I doing something wrong?

答案1

得分: 3

byte[] raw = new byte[]{};
String msg = new String(raw, StandardCharsets.US_ASCII);
英文:
byte[] raw = new byte[]{};
String msg = new String(raw, StandardCharsets.US_ASCII);

答案2

得分: 0

Explanation

问题在于您可能会写new String(raw, "nonsensefoobar"),这显然是没有意义的。

因此,Java强制您告诉它如何处理这种编码方案不存在的特殊情况,要么通过try-catch它,要么通过声明throws

public void myMethod(){
    ...
    try {
        String msg = new String(raw, "US-ASCII");
    ...
    } catch (UnsupportedEncodingException e) {
        ... // 处理问题
    }
    ...
}

// 或者

public void myMethod() throws UnsupportedEncodingException {
    ...
    String msg = new String(raw, "US-ASCII");
    ...
}

这是一个非常普通和常见的异常情况,我建议学习异常处理

更好的解决方案

与指定编码方案为字符串然后处理异常不同,您可以使用构造函数的另一个重载,该重载接受Java知道存在的预定义方案,因此它不会麻烦您处理异常:

String msg = new String(raw, StandardCharsets.US_ASCII);
英文:

Explanation

The problem is that you could be writing new String(raw, "nonsensefoobar") which obviously is non-sense.

Hence Java forces you to tell it how you want to deal with the exceptional case that this encoding scheme does not exist. Either by try-catching it or by declaring throws:

public void myMethod(){
    ...
    try {
        String msg = new String(raw, "US-ASCII");
    ...
    } catch (UnsupportedEncodingException e) {
        ... // handle the issue
    }
    ...
}

// or

public void myMethod() throws UnsupportedEncodingException {
    ...
    String msg = new String(raw, "US-ASCII");
    ...
}

That is a super ordinary and common exception-situation, I would suggest to learn about exceptions.


Better solution

Instead of specifying the encoding scheme as string and then dealing with exceptions, you can use another overload of the constructor which takes in predefined schemes for which Java knows that they exist, so it will not bother you with exceptions:

String msg = new String(raw, StandardCharsets.US_ASCII);

huangapple
  • 本文由 发表于 2020年8月14日 16:51:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63409549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定