创建一个自定义的异常类,该类继承Exception类,为该类写一个构造方法

2025-01-03 00:28:38
推荐回答(1个)
回答1:

public class MyException extends Exception ...{

public MyException(String msg){
super(msg);
}

}
public class TestMyException ...{

public static void MyException() throws MyException{
throw new MyException("\"MyException()\" method occurs an exception! ");
}

public static void main(String[] args) ...{
try...{
TestMyException.MyException();
} catch(MyException e1) ...{
System.out.println("Exception: " + e1.getMessage());
e1.printStackTrace();
}
}
}