1.创建一个自定义异常类,继承Exception或RuntimeException;
//1.继承Exception或RuntimeException异常
public class CustomException extends Exception {
//2.给出有参构造,super调用
public CustomException(String message) {
super(message);
}
}
2.创建一个方法抛出自定义异常CustomException,调用抛出自定义异常的方法
public class ExceptionTest{
public static void main(String[] args) {
//4.调用方法
try {
showException();
} catch (CustomException e) {
e.printStackTrace();
}
}
//3.创建一个方法抛出自定义异常CustomException
public static void showException() throws CustomException{
throw new CustomException("使用自定义异常");
}
}