簡體   English   中英

嘗試使用Integer.parseInt()語法錯誤進行捕獲[Java]

[英]Try and Catch using Integer.parseInt() syntax error [Java]

我正在嘗試制作一個簡單的方法來測試以查看提供的String是否僅包含數字,為此,我嘗試在嘗試嘗試的地方使用try和catch(只是學習了它,我想練習將其使用) parseInt()給定的String,如果有錯誤(不是數字),它將捕獲該錯誤並返回false;

    public boolean checkNumber(String s){
    try(Integer.parseInt(s)){
        return true;
    }
    catch(Exception E){
        return false;
    }
}

它說我的構造器放錯了位置。

捕獲正確的異常,然后將try檢查插入塊中,而不是放在方括號中。

 try {
       Integer.parseInt(s);
       return true;
    }
    catch(NumberFormatException e){
        return false;
    }

那不是嘗試的正確語法。 采用

try
{
    Integer.parseInt(s);
    return true;
}
catch (Exception ex)
{
    return false;
}

語法錯誤

try {
  Integer.parseInt(s);
} catch(Exception e) {
  return false;
}
return true;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM