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就不会编译出错?
public static void parse(String str){
try{
Float f = Float.parseFloat(str);
}catch(NumberFormatException Exception){
f=0;
}finally{
System.out.println(f);
}
}
怎么会编译出错的?但是不用try-catch就不会编译出错?
可以把f定义提到try/catch块外面来。
public static void parse(String str){
Float f;
try{
f = Float.parseFloat(str);
}catch(NumberFormatException Exception){
f=0;
}finally{
System.out.println(f);
}
}