DAY7第七天的关于异常的练习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DAY7第七天的关于异常的练习相关的知识,希望对你有一定的参考价值。
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()); } } }
本文出自 “yehomlab” 博客,请务必保留此出处http://yehom.blog.51cto.com/5159116/1784239
以上是关于DAY7第七天的关于异常的练习的主要内容,如果未能解决你的问题,请参考以下文章