Java自定义异常

java

package cn.Douzi.Lab7_4;

public class Triangle {

int a, b, c;

public Triangle(int a, int b, int c) {

super();

this.a = a;

this.b = b;

this.c = c;

}

int getArea() throws Exception {

if (a + b <= c || a + c <= b || b + c <= a) {

throw new IllegalArgumentException("两边之和大于第三边");

}

else {

int p = (a + b + c) / 2;

return (int)Math.sqrt(p * (p - a) * (p - b) * (p - c));

}

}

}

package cn.Douzi.Lab7_4;

public class Lab4 {

public static void main(String[] args) {

// TODO Auto-generated method stub

Triangle tri = new Triangle(5, 1, 4);

try {

int area;

area = tri.getArea();

System.out.println("面积: " + area);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

}

}

}

以上是 Java自定义异常 的全部内容, 来源链接: utcz.com/z/391335.html

回到顶部