如何捕获NumberFormatException?

时间:2019-03-01 20:31:40

标签: java numberformatexception

我正在尝试找出如何在代码中捕获numberformat异常错误,以便如果用户在字符串中输入字母,并且我的程序尝试将其解析为int,则我的程序不会引发错误但请停止并返回一个布尔值。我还试图了解,如果try语句有效,我希望它继续执行以下代码。

    if (counter == 3) {
        int compare;
        boolean check = true;
        String[] newip = IpAddress.split("\\.");
        if (newip.length == 4) {
            for (int index = 0; index < newip.length; index++) {
                //There should be a try statement here.
                // if the try statement fails then I'd like for it to catch
                // the numberformatexception and evaluate my boolean to 
                //false;
                //but if it passes I'd like for it to continue to execute 
                //the following code.
                    compare = Integer.parseInt(newip[index]);
                if (compare >= 0 & (compare <= 255)) {
                    check = true;
                }
                else{
                    check = false;
                }
            }
            if (check)
                return true;
            else
                return false;
        }
        else {
            check = false;
            return check;
        }
    }
    else{
        return false;
    }
}

4 个答案:

答案 0 :(得分:1)

使用try / catch包围该行:

try {
    compare = Integer.parseInt(newip[index]);
} catch (NumberFormatException e) {
    check = false;
}

然后:

if (check) {
    if (compare >= 0 & (compare <= 255)) {
        check = true;
    } else {
        check = false;
    }
} else {
    return false;
}

答案 1 :(得分:0)

看看https://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html会显示NumberFormatException是RunTimeException,因此不必总是被捕获。如果您期望某些意外的事情,最好将整个代码放在try-catch-block中,这样当出现问题时,整个事务将被中止,因此至少可以保证您的代码是原子的(全部或全部) )。

try {
 if (counter == 3) {
  int compare;
  boolean check = true;
  String[] newip = IpAddress.split("\\.");
  if (newip.length == 4) {
   for (int index = 0; index < newip.length; index++) {
    //There should be a try statement here.
    // if the try statement fails then I'd like for it to catch
    // the numberformatexception and evaluate my boolean to 
    //false;
    //but if it passes I'd like for it to continue to execute 
    //the following code.
    compare = Integer.parseInt(newip[index]);
    if (compare >= 0 & (compare <= 255)) {
     check = true;
    } else {
     check = false;
    }
   }
   if (check)
    return true;
   else
    return false;
  } else {
   check = false;
   return check;
  }
 } else {
  return false;
 }
}
} catch (NumberFormatException e) {
 System.err.printf(e.message());
}

答案 2 :(得分:0)

您可以使用以下示例代码来解析整数:

try {
    Integer.parseInt(newip[index]);
    //this runs only if converting is successful
    //put your code here ....
    } catch (NumberFormatException e) {
    //error parsing to int
    //handle your error here
    check=false;
}

答案 3 :(得分:0)

您可以使用NumberUtils 3.x中的commons-lang来检查输入是否为数字,而不是捕获NumberFormatException。

NumberUtils.isNumber(newip[index])

但是根据文档,它会在4.x中弃用,您需要使用isCreatable

NumberUtils.isCreatable(newip[index])