程序开发中,格式转换的时候,经常由于字符串可能是其他的不可预知的符号导致,字符串转数值失败,

 

这个时候可以妙用try catch来解决,如下图所示。其实,很多其他不可预知的异常情况,也可以用它来处理。

 

public static int StringToInt(String s)
    {
        try 
        {
            return Integer.parseInt(s);
        } 
        catch (NumberFormatException e) 
        {
            return -999;
        }
    }

 

相关文章:

  • 2022-12-23
  • 2021-11-15
  • 2021-07-16
  • 2022-12-23
  • 2022-12-23
  • 2022-01-31
  • 2021-08-19
  • 2021-06-20
猜你喜欢
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案