class test2{
public static void parse(String str){
try{
Float f = Float.parseFloat(str);
}catch(NumberFormatException Exception){
f=0;
}finally{
System.out.println(f);
}
}
怎么会编译出错的?但是不用try-catch就不会编译出错?

解决方案 »

  1.   

    f 作用域里...catch和finally里面已经访问不到f了。
    可以把f定义提到try/catch块外面来。
      

  2.   

    class test2{
    public static void parse(String str){
           Float f;
            try{
                f = Float.parseFloat(str);
            }catch(NumberFormatException Exception){
                f=0;
            }finally{
                System.out.println(f);
            }
        }