![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
如何使用Java创建自己的异常子类
1个回答
展开全部
package com.yl.main;
public class TestException extends Exception {
public TestException(String msg){
super(msg);
}
public static void main(String[] args) {
int num = -1;
try {
TestException.printNumber(num);
} catch (TestException e) {
e.printStackTrace();
}
}
public static void printNumber(int num) throws TestException{
if(num<=0){
throw new TestException("自定义异常处理");
}else{
System.out.println(num);
}
}
}
运行结果:
com.yl.main.TestException: 自定义异常处理
at com.yl.main.TestException.printNumber(TestException.java:21)
at com.yl.main.TestException.main(TestException.java:12)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询