开发者社区> 问答> 正文

java自定义异常中如何定义AddException?

已解决

java自定义异常中如何定义AddException?

展开
收起
游客gzyuldo4mrg6i 2022-04-03 21:11:11 1030 0
1 条回答
写回答
取消 提交回答
  • 推荐回答

    class AddException extends Exception {

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

    }

    public class TestDemo {

    public static void main(String args[]) {
    
        int num = 20;
    
        try {
    
            if(num > 10) { // 出现了错误,应该产生异常
    
                throw new AddException("数值传递的过大!");
    
            }
    
        } catch(Exception e) {
    
            e.printStackTrace();
    
        }
    
    }
    

    }

    输出结果:

    AddException:数值传递的过大!...

    2022-04-03 21:39:08
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载