/**自定义异常类**/
public class MyException extends Exception {
public MyException() {
super();
}
public MyException(String message, Throwable cause) {
super(message, cause);
}
public MyException(String message) {
super(message);
}
public MyException(Throwable cause) {
super(cause);
}
}
/**调用测试**/
public class Test9 {
public static void main(String[] args) throws Exception {
TheTest t = new TheTest();
try {
int num = t.getNum(6);
} catch (Exception e) {
// 1.捕获打印控制台
e.printStackTrace();
// 2.抛给主方法,主方法抛给jvm
throw e;
}
}
}
class TheTest {
public int getNum(int a) throws MyException {
if (a < 5) {
return 1;
} else {
throw new MyException("自定义错误..");
}
}
}