在此方法中输入文本时
private int random1DimGetNumeral(String textThatCausesError){
int result = 2;
try{
result = Integer.parseInt(textThatCausesError);
} catch (Exception inputError) {
textPanel.setText("Input Error for Random Dimmension feild 1.\n");
}
return result;
}
表示参数textThatCausesError,稍后当文本为数字时,为“56”。它仍然捕获异常并返回2。难道我的捕获太普通了吗?
答案 0 :(得分:2)
Integer.parseInt(textThatCausesError);
是正确的,获得例外的唯一选择是:
答案 1 :(得分:1)
这适合我。
class Pong {
public static void main(String[] args){
Pong pong = new Pong();
System.out.println(pong.random1DimGetNumeral("56"));
}
private int random1DimGetNumeral(String textThatCausesError){
int result = 2;
try{
result = Integer.parseInt(textThatCausesError);
} catch (Exception inputError) {
System.out.println("Input Error for Random Dimmension feild 1.\n");
}
return result;
}
}