public class ExceptionTest3 {
public void go(int n) throws ArithmeticException {
int a = 9;
System.out.println(a = a / n);
} public static void main(String args[]) throws ArithmeticException{
ExceptionTest3 t = new ExceptionTest3();
t.go(0);
}
}
public void go(int n) throws ArithmeticException {
int a = 9;
System.out.println(a = a / n);
} public static void main(String args[]) throws ArithmeticException{
ExceptionTest3 t = new ExceptionTest3();
t.go(0);
}
}
public void go(int n) {
int a = 9;
System.out.println(a = a / n);
throw new ArithmeticException("fdgve"); //这个throw怎么有何没有结果都是一回事啊!! } public static void main(String args[]) {
ExceptionTest2 t = new ExceptionTest2();
try {
t.go(0);
} catch (ArithmeticException ae) {
ae.printStackTrace();
}
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ExceptionTest3.go(ExceptionTest3.java:4)
at ExceptionTest3.main(ExceptionTest3.java:9)第二段代码:异常应该捕获。
try{ System.out.println(a = a / n);
}catch(ArithmeticException){
throw new ArithmeticException("fdgve");
}
像楼主的代码,当传入的参数非0时,抛出的异常就是throw new语句抛出来的,它会输出"fdgve"信息。而传入0时,抛出的异常是由语句println抛出的。
public void go(int n) {
int a = 9;
try {
System.out.println(a = a / n);
}catch( ArithmeticException e){
System.out.println("除数不能为0");
}
} public static void main(String args[]) {
ExceptionTest3 t = new ExceptionTest3();
t.go(0);
}
}
int a = 9;
if(n == 0)
throw new ArithmeticException("fdgve");
System.out.println(a = a / n);
}
public class ExceptionTest2 {
public void go(int n) throws ArithmeticException {
int a = 9;
// 这样写大哥
try {
System.out.println(a = a
/ n);
} catch (Exception e) {
throw new ArithmeticException("fdgve");
}
} public static void main(String args[]) {
ExceptionTest2 t = new ExceptionTest2();
try {
t.go(0);
} catch (ArithmeticException ae) {
ae.printStackTrace();
}
}
}