Try块中的语句执行

时间:2016-02-21 13:19:53

标签: java try-catch

这是Exception类:

public class IllegalValuesException extends Exception {
    public IllegalValuesException(String message) {
        super(message);
    }
    public IllegalValuesException() {
        super("Illegal Values");
    }
}

这是Tringle课程:

public class Triangle {
    private double a, b, c;
    public Triangle(double x, double y, double z) throws IllegalValuesException {
        if(x+y > z && y+z > x && x+z > y) {
            a = x; b = y; c = z;
        }
        else
            throw new IllegalValuesException("Error: Values "+x+", "+y+", "+z+ " do not make a valid triangle");
    }

    public double area() {
        double s = this.perimeter()/2.0;
        return Math.sqrt(s*(s - a)*(s - b)*(s - c));
    }

    public double perimeter() {
        return a + b + c;
    }
}

这是我的主要课程:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Triangle[] t = new Triangle[3];
        try {
            t[0] = new Triangle(6, 6, 6);
            t[1] = new Triangle(1, 4, 1);
            t[2] = new Triangle(4, 4, 4);
        }
        catch(IllegalValuesException e1) {
            System.out.println(e1.getMessage());

        }

        try {
            Scanner s = new Scanner(System.in);
            System.out.println("Enter an integer (from 1 to 3): ");
            int val = s.nextInt();

            System.out.println("Triangle: Area:" + t[val - 1].area() + " Perimeter: " +t[val -1].perimeter());
        }
        catch(InputMismatchException e2) {
            System.out.println("You entered a non-integer!");
        }
        catch(ArrayIndexOutOfBoundsException e3) {
            System.out.println("You entered a value which is not in the range 1-3");
        }
        catch(NullPointerException e4) {
            System.out.println("Sorry this triangle does not exist since it had illegal values");
        }
    }
}

当我运行它时,Tringle t [2]不起作用,我得到了这个:

Error: Values 1.0, 4.0, 1.0 do not make a valid triangle

Enter an integer (from 1 to 3): 
3

Sorry this triangle does not exist since it had illegal values

它永远不会到达t[2],并且由于错误而停在t[1]。 我想编辑Tringle类中的Exception:

public Triangle(double x, double y, double z) throws IllegalValuesException {
    if (x + y > z && y + z > x && x + z > y) {
        a = x;
        b = y;
        c = z;
    } else
        throw new IllegalValuesException("Error: Values " + x + ", " + y + ", " + z + " do not make a valid triangle");
}

以这种方式我可以到达t[2]

2 个答案:

答案 0 :(得分:1)

如果在try块的主体中​​发生异常,则控制立即转移(跳过try块中的其余语句)到catch块。一旦catch块完成执行,然后最终阻塞并在该程序的其余部分之后。 source

仅执行t[0] = new Triangle(6, 6, 6);t[1] = new Triangle(1, 4, 1);,第二个会抛出错误,因此会跳转到catch部分。

您告诉编译器您要尝试创建3个三角形并可能会出现异常。因此,当遇到异常时,他会移动到catch部分并检查它是否是您预期的异常。

如果你想尝试创建每个三角形,你必须用try catch包围每个语句:

try {
    t[0] = new Triangle(6, 6, 6);
}
catch(IllegalValuesException e1) {
    System.out.println(e1.getMessage());

} 

try {
    t[1] = new Triangle(1, 4, 1);
}
catch(IllegalValuesException e1) {
    System.out.println(e1.getMessage());

}

try {
    t[2] = new Triangle(4, 4, 4);
}
catch(IllegalValuesException e1) {
    System.out.println(e1.getMessage());

}

答案 1 :(得分:0)

我不确定 我想编辑Tringle类中的异常 意味着但问题出在条件检查if (x + y > z && y + z > x && x + z > y) 。它会为false返回Triangle(1, 4, 1)(1 + 1不大于4)并执行else并获得IllegalValuesException

如果您不希望异常停止您的计划,请不要使用例外,只需打印即可

if (x + y > z && y + z > x && x + z > y) {
    a = x;
    b = y;
    c = z;
}
else {
    System.out.println("Error: Values " + x + ", " + y + ", " + z + " do not make a valid triangle");
}