public class p { public static void main(String[] args) { Triangle k=new Triangle(); try { k.getArea(3,4,5); k.showInfo(5,6,3); k.getArea(1,1,4); k.showInfo(1,1,3); } catch (NotTriangleException e) { e.printStackTrace(); }finally { System.out.println("程序结束了!"); } }}class NotTriangleException extends Exception{ public NotTriangleException (int x,int y,int z){ super("边长为"+x+y+z+"不能构成三角形"); }} class Triangle{ int x,y,z; int area; public Triangle(int x,int y,int z){ this.x=x; this.y=y; this.z=z; } public Triangle() { } public void getArea(int x,int y,int z)throws NotTriangleException{ double p = (x + y + z) / 2.0; if(x+y<=z||x+z<=y||y+z<=x){ throw new NotTriangleException(x,y,z); } System.out.println("面积:"+Math.sqrt(p * (p - x) * (p - y) * (p - z))); }
public void showInfo(int x,int y,int z)throws NotTriangleException{ if(x+y<=z||x+z<=y||y+z<=x){ throw new NotTriangleException(x,y,z); } System.out.println("边长:"+x+y+z); }}
参考如下:
// e.printStackTrace();
System.out.println(e.getMessage());
如有帮助,欢迎采纳哈!
和哪个上面的一样的输出啊,没看明白,你的程序运行了下,就会输出这个图片上的信息啊