java中 try catch的妙用

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

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

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

猜你喜欢

转载自www.cnblogs.com/tiandi/p/10872750.html