class MyException extends Exception {
private int id;
public MyException(String message, int n) {
super(message);
id = n;
}
public int getId() {
return id;
}
}public class Exception1 {
public void regist(int i) throws MyException {
if(i < 0) {
new MyException("不能小于0地", 3);
}
System.out.println("登记人数:" + i);
}
public void m() {
try {
regist(-10);
} catch (MyException e) {
System.out.println("出错了" + e.getId());
e.printStackTrace();
}
System.out.println("操作结束");
}
public static void main(String args[]) {
Exception1 a = new Exception1();
a.m();
}}
这里说class MyException extends Exception 的 MyException是黄线,没用到,这是怎么回事,不论怎样运行都会,我是在eclipse下做的登记人数:-10
操作结束
The serializable class MyException does not declare a static final serialVersionUID field of type long ja/src Exception1.java 第 1 行 1252243258781 3866
public void regist(int i) throws MyException {
if(i < 0) {
throw new MyException("不能小于0地", 3);
}
System.out.println("登记人数:" + i);
}
然后就结束了。
catch里面的肯定不打印
因为你只是new 了个MyException
而不是抛出
呵呵,原来是没throw