有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Try块中执行java语句

这是异常类:

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类中的异常:

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]


共 (1) 个答案

  1. # 1 楼答案

    如果try块的主体中发生异常,则控制立即转移(跳过try块中的其余语句)到catch块。一旦catch block完成了执行,然后最终阻塞,然后是程序的其余部分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());
    
    }