class WeightTooMuchException extends RuntimeException { private String message; public WeightTooMuchException(String message) { this.message=message; } public String getMessage() { return message; } public void setMessage(String message) { this.message=message; } } class WeightTooLowException extends RuntimeException { private String message; public WeightTooLowException(String message) { this.message=message; } public String getMessage() { return message; } public void setMessage(String message) { this.message=message; } } class Man { private int weight; public void setWeight(int weight) //throws WeightTooMuchException,WeightTooLowException { if(weight > 100) { throw new WeightTooMuchException("You are too fat"); } else if(weight < 50) { throw new WeightTooLowException("You are too thin"); } this.weight=weight; } public int getWeight() { return weight; } } class WeightDemo2 { public static void main(String[] args) { Man man=new Man(); try { man.setWeight(40); } catch(WeightTooLowException ex) { System.out.println(ex.getMessage()); } catch(WeightTooMuchException ex) { System.out.println(ex.getMessage()); } } }
【DAY7】第七天的关于异常的练习
原创
©著作权归作者所有:来自51CTO博客作者yehom的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:【DAY8】关于多线程的学习笔记
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
学习第七天
好好学
i++ n的阶乘 #include -
打卡第七天
今天我把自己的公众号弄了一下,整出了一个自动回复机器人,因为只花了一天时间还比较粗糙,后面等我整理好
微信公众号 自动回复 代码分享 公众号 -
学习第七天,
人每日总结
人每日总结