目录
1、自定义TriangleException类
2、修改Triangle类
3、Demo示例
1、自定义TriangleException类
public class TriangleException extends Exception {
public TriangleException() {
}
public TriangleException(String message) {
super(message);
}
}
2、修改Triangle类
public Triangle(double side1,double side2,double side3)
throws TriangleException {
if((side1+side2)>side3&&(side1+side3)>side2&&(side2+side3)>side1){
this.side1=side1;this.side2=side2;this.side3=side3;}
else
throw new TriangleException("the wrong name!");
}
3、Demo示例
public class Test1 {
public static void main(String[] args) {
try {
Triangle tri = new Triangle(2, 4, 6);
} catch (TriangleException ex) {
ex.printStackTrace();
}
}
}
PackageP12.TriangleException: the wrong name!
at PackageP11.Triangle.<init>(Triangle.java:14)
at PackageP12.Test1.main(Test1.java:8)
Process finished with exit code 0
|