我遇到这种情况,我需要解析String到int,我不知道该怎么做了NumberFormatException.当我没有抓住它时,编译器不会抱怨,但我只是想确保我正确处理这种情况.
private int getCurrentPieceAsInt() {
int i = 0;
try {
i = Integer.parseInt(this.getCurrentPiece());
} catch (NumberFormatException e) {
i = 0;
}
return i;
}
Run Code Online (Sandbox Code Playgroud)
我想简化这样的代码.编译器没有问题,但线程死了NumberFormatException.
private int getCurrentPieceAsInt() {
int i = 0;
i = Integer.parseInt(this.getCurrentPiece());
return i;
}
Run Code Online (Sandbox Code Playgroud)
Google CodePro希望我以某种方式记录异常,我同意这是最佳做法.
private int getCurrentPieceAsInt() {
int i = 0;
try {
i = Integer.parseInt(this.getCurrentPiece());
} catch (NumberFormatException e) {
i = 0;
e.printStackTrace();
}
return i;
}
Run Code Online (Sandbox Code Playgroud)
我希望此方法0在当前片段不是数字或无法解析时返回.当我没有 …