Java中处处皆对象,所以异常也是一个对象,可以把异常作为RuntimeException的参数:
throw new RuntimeException(e); 抛出运行时异常,如下:[code]public class TestException {
public static void main(String[] args) {
try {
/* 当要解析的字符串中包含非数字时,会抛出java.lang.NumberFormatException异常,如abc */
Integer.parseInt("123");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}[/code]直接throw e的话,表示向上抛出异常,要在方法声明中抛出[code]public class TestException {
public static void main(String[] args) {
try {
test();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test() throws Exception {
try {
/* 当要解析的字符串中包含非数字时,会抛出java.lang.NumberFormatException异常,如abc */
Integer.parseInt("aa123");
} catch (Exception e) {
throw e;
}
}
}[/code] |