package com.TopWise.test;public class Test {
public static void main(String args[]) {
try {
System.out.println(Test.StringToInt("2222a"));
} catch (NumberFormatException ex) {
throw new NumberFormatException("转换有错误" + ex.getMessage());
}
System.out.println("abcdefg");//不能打印出来
}
public static int StringToInt(String a) {
return Integer.parseInt(a);
}
}我这样保护起来,但为什么还是不能打印出abcdefg
然后抛出
java.lang.NumberFormatException: 转换有错误For input string: "2222a"
at com.TopWise.test.Test.main(Test.java:8)
Exception in thread "main"
public static void main(String args[]) {
try {
System.out.println(Test.StringToInt("2222a"));
} catch (NumberFormatException ex) {
throw new NumberFormatException("转换有错误" + ex.getMessage());
}
System.out.println("abcdefg");//不能打印出来
}
public static int StringToInt(String a) {
return Integer.parseInt(a);
}
}我这样保护起来,但为什么还是不能打印出abcdefg
然后抛出
java.lang.NumberFormatException: 转换有错误For input string: "2222a"
at com.TopWise.test.Test.main(Test.java:8)
Exception in thread "main"
public static void main(String args[]) {
System.out.println(Test.StringToInt("2222a"));
System.out.println("aaaaaaaaaa");
}
public static int StringToInt(String a) throws NumberFormatException {
return Integer.parseInt(a);
}
}
以及下面这样 效果一样呢????public class Test {
public static void main(String args[]) {
System.out.println(Test.StringToInt("2222a"));
System.out.println("aaaaaaaaaa");
}
public static int StringToInt(String a) {
try{
return Integer.parseInt(a);
}catch(NumberFormatException ex){
throw new NumberFormatException("转换有错误" + ex.getMessage()); }
}
}
System.out.println(Test.StringToInt("2222a"));
} catch (NumberFormatException ex) {
// 放到里面! catch中又抛出异常,自然就打印不了
System.out.println("abcdefg");//不能打印出来throw new NumberFormatException("转换有错误" + ex.getMessage());}