1. 程式人生 > >Java自定義異常

Java自定義異常

trace tex logs fin rate cat java自定義異常 leg pan

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自定義異常