程序开发中,格式转换的时候,经常由于字符串可能是其他的不可预知的符号导致,字符串转数值失败,
这个时候可以妙用try catch来解决,如下图所示。其实,很多其他不可预知的异常情况,也可以用它来处理。
public static int StringToInt(String s) { try { return Integer.parseInt(s); } catch (NumberFormatException e) { return -999; } }
本文共 353 字,大约阅读时间需要 1 分钟。
程序开发中,格式转换的时候,经常由于字符串可能是其他的不可预知的符号导致,字符串转数值失败,
这个时候可以妙用try catch来解决,如下图所示。其实,很多其他不可预知的异常情况,也可以用它来处理。
public static int StringToInt(String s) { try { return Integer.parseInt(s); } catch (NumberFormatException e) { return -999; } }
转载于:https://www.cnblogs.com/tiandi/p/10872750.html
转载地址:https://blog.csdn.net/weixin_30807779/article/details/98189337 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!