设计两个异常类,用于检查身高,当输入身高低于170时产生TooShortException异常,而当输入身高高于180时产生TooTallException异常。编写一个检查类,其中的有一个方法用于检查身高,身高符合条件返回True,否则根据情况产生以上两种异常之一。编写程序测试上述内容要求。
求源代码和运行截图
public class TooShortException extends Exception{
public TooShortException(String message) {
super(message);
}
}
public class TooTallException extends Exception{
public TooTallException(String message) {
super(message);
}
}
public class Check {
public static void main(String[] args){
try {
boolean flag=isHeigh(183);
if (flag){
System.out.println("身高正常");
}
}catch (Exception e){
e.printStackTrace();
}
}
public static boolean isHeigh(int height) throws Exception{
if (height<170){
throw new TooShortException("身高过矮");
}else if (height>180){
throw new TooTallException("身高过高");
}
return true;
}
}
这个不难,有疑问,
欢迎来交流。
包解决