简体   繁体   English

异常处理

[英]Exception Handling

I'm writing a program that is supposed to create a triangle and throw an exception if the triangle is not legal, meaning that the sum of any two sides must be greater than the length of the remaining side. 我正在编写一个程序,该程序应该创建一个三角形并在三角形不合法的情况下引发异常,这意味着任何两个边的总和必须大于剩余边的长度。 The program consists of 4 classes (the super class GeometricObject, a Triangle class, a Program6 class, and an IllegalTriangleException class). 该程序包含4个类(超类GeometricObject,Triangle类,Program6类和IllegalTriangleException类)。 The program is supposed to read in data from a file that consists of the lengths for each side, the color, and whether it's filled or not, perform calculations to determine the perimeter and area, and write the information to a file entitled prog6.out. 该程序应该从文件中读取数据,该文件包括各边的长度,颜色以及是否填充,执行计算以确定周长和面积,并将信息写入名为prog6.out的文件中。 。 I haven't implemented the writing to a file yet though since I'm having some problems getting the program to display the results correctly using a regular print statement since implementing the IllegalTriangleException class. 自实现IllegalTriangleException类以来,由于在使程序使用常规的打印语句正确显示结果时遇到一些问题,因此我尚未实现对文件的写入。

Here is what I have so far: The GeometricObject Class: 这是我到目前为止所拥有的:GeometricObject类:

public class GeometricObject {
    private String color = "white";
    private boolean filled;
    private java.util.Date dateCreated;

    public GeometricObject() {
        dateCreated = new java.util.Date();
    }

    public GeometricObject(String color, boolean filled) {
        dateCreated = new java.util.Date();
        this.color = color;
        this.filled = filled;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    public java.util.Date getDateCreated() {
        return dateCreated;
    }

    public String toString() {
        return "Created on: " + dateCreated + "\nColor: " + color + "\nFilled: " + filled;
    }
}

The Triangle class: 三角类:

public class Triangle extends GeometricObject {
    private double side1;
    private double side2;
    private double side3;
    private double s = (side1 + side2 + side3)/2;
    private double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
    private double perimeter;

    public Triangle() {
        side1 = 1.0;
        side2 = 1.0;
        side3 = 1.0;
    }

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    public double getSide1() {
        return side1;
    }

    public double getSide2() {
        return side2;
    }

    public double getSide3() {
        return side3;
    }



    public double getPerimeter() {
        return (side1 + side2 + side3);
    }

    public double getArea() {
        this.s = (side1 + side2 + side3)/2;
        this.area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
        return area;
    }

    public String toString() {
        return "Triangle: " + "\nSide 1: " + side1 + "\nSide2: " + side2 + "\nSide3: " + side3 + "\nArea: " + getArea() + "\nPerimeter: " + getPerimeter() + "\nColor: " + getColor() + "\nFilled: " + isFilled();
    }

    public void setSides(double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
        if (side1 + side2 > side3){
            if (side1 + side3 > side2)
                if (side2 + side3 > side1)
        side1 = newSide1;
        side2 = newSide2;
        side3 = newSide3;
        }
        else
            throw new IllegalTriangleException(newSide1, newSide2, newSide3);
    }

    public Triangle(double side1, double side2, double side3, String color, boolean filled) throws IllegalTriangleException {
        try {
            setSides(getSide1(), getSide2(), getSide3());
        }
        catch(IllegalTriangleException ex) {
            System.out.println("Illegal Triangle: The summ of any two sides must be greater than the other side" + side1 + "," + side2 + "," + side3);
        }
    }
}

The Program6 class: Program6类:

import java.util.Scanner; 

public class Program6 {
    public static void main(String[] args) throws Exception {
        java.io.File file = new java.io.File("prog6.dat");
        Scanner fin = new Scanner(file);

        while (fin.hasNext()) {
            double side1 = fin.nextDouble();
            double side2 = fin.nextDouble();
            double side3 = fin.nextDouble();
            String color = fin.next();
            String bool = fin.next();
            boolean filled;
             if(bool.equals("T"))
                  filled = true;
             else
                  filled = false;
            Triangle triangle = new Triangle(side1, side2, side3, color, filled);
            System.out.println(triangle);
        }
        fin.close();
    }
}

And the IllegalTriangleException class: 和IllegalTriangleException类:

public class IllegalTriangleException extends Exception {

    private double side1;
    private double side2;
    private double side3;

    public IllegalTriangleException() {
    }

    public IllegalTriangleException(double side1, double side2, double side3) {
        super("Illegal Triangle: The summ of any two sides must be greater than the other side" + side1 + "," + side2 + "," + side3);
        //this.side1 = side1;
        //this.side2 = side2;
        //this.side3 = side3;
    }

    public double getSide1() {
        return side1;
    }

    public double getSide2() {
        return side2;
    }

    public double getSide3() {
        return side3;
    }
}

And in case it helps, here is the file that is being used for the input: 如果有帮助,这里是用于输入的文件:

1.0 1.5 1.0 yellow  T
3.0 4.0 5.0 blue F
2.4 1.1 1.2 red T
3.1 6.2 8.5 green T
1.2 9.9 4.5 cyan F

Before adding in the Exception class, the program was working properly, the file was being read correctly and assigning the correct values to the variables, and the area and perimeters were being calculated correctly, however, now that I've added in the exception class, here is my output: 在添加Exception类之前,程序运行正常,正在正确读取文件并将正确的值分配给变量,并且正确地计算了面积和周长,但是,现在,我已经在Exception类中添加了,这是我的输出:

Illegal Triangle: The summ of any two sides must be greater than the other side1.0,1.5,1.0
Triangle: 
Side 1: 0.0
Side2: 0.0
Side3: 0.0
Area: 0.0
Perimeter: 0.0
Color: white
Filled: false
Illegal Triangle: The summ of any two sides must be greater than the other side3.0,4.0,5.0
Triangle: 
Side 1: 0.0
Side2: 0.0
Side3: 0.0
Area: 0.0
Perimeter: 0.0
Color: white
Filled: false
Illegal Triangle: The summ of any two sides must be greater than the other side2.4,1.1,1.2
Triangle: 
Side 1: 0.0
Side2: 0.0
Side3: 0.0
Area: 0.0
Perimeter: 0.0
Color: white
Filled: false
Illegal Triangle: The summ of any two sides must be greater than the other side3.1,6.2,8.5
Triangle: 
Side 1: 0.0
Side2: 0.0
Side3: 0.0
Area: 0.0
Perimeter: 0.0
Color: white
Filled: false
Illegal Triangle: The summ of any two sides must be greater than the other side1.2,9.9,4.5
Triangle: 
Side 1: 0.0
Side2: 0.0
Side3: 0.0
Area: 0.0
Perimeter: 0.0
Color: white
Filled: false

Not sure what I've done wrong, if anyone can help me out it'd be much appreciated. 不知道我做错了什么,如果有人可以帮助我,将不胜感激。 This is the first time I've worked with this many classes and also the first time I've worked with exception handling, so please be nice :-) 这是我第一次使用这么多的类,也是我第一次使用异常处理,所以请加心:-)

There are three basic problems... 有三个基本问题...

First... 第一...

In your Triangle constructor, you are passing getSide1() , getSide2() and getSide3() to the setSides method... 在您的Triangle构造函数中,您getSide1()getSide2()getSide3()传递给setSides方法。

public Triangle(double side1, double side2, double side3, String color, boolean filled) throws IllegalTriangleException {
    try {
        setSides(getSide1(), getSide2(), getSide3());

This means, you are simply passing the values of the fields side1 , side2 and side3 , which haven't been set yet are defaulted to 0 , which means you are effectively saying... 这意味着,你只是传递字段的值side1side2side3 ,已尚未设置的预设值都是0 ,这意味着你实际上是说...

setSides(0d, 0d, 0d);

This needs to be changed to pass the parameters of the constructor, for example... 这需要更改以传递构造函数的参数,例如...

public Triangle(double side1, double side2, double side3, String color, boolean filled) throws IllegalTriangleException {
    try {
        setSides(side1, side2, side3);

Second... 第二...

Your setSides method is testing the state of the instance fields state1 , state2 and state3 , which haven't been set yet (and even correcting for the previous mistake) are still defaulted to 0 ...so 0 + 0 > 0 is ... false 您的setSides方法正在测试实例字段state1state2state3的状态,这些实例字段尚未设置(甚至更正了先前的错误)仍默认为0 ...所以0 + 0 > 0为.. false

public void setSides(double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
    if (side1 + side2 > side3){
        if (side1 + side3 > side2)
            if (side2 + side3 > side1)

This should be modified to use the values passed to the method instead... 应该修改它以使用传递给方法的值代替...

public void setSides(double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
    if (newSide1 + newSide2 > newSide3)
        if (newSide1 + newSide3 > newSide2)
            if (newSide2 + newSide3 > newSide1)

Third... 第三...

The previous if statement is wrong. 前面的if语句错误。 What this basically equates to saying is, if everything is fine, set side1 = newSide1 and regardless of anything else, set side2 and side3 to newSide2 and newSide3 respectivly... 什么这基本上相当于一说的是,如果一切正常,设置side1 = newSide1且不论别的,设置side2side3newSide2newSide3 respectivly ...

public void setSides(double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
    if (newSide1 + newSide2 > newSide3) 
        if (newSide1 + newSide3 > newSide2)
            if (newSide2 + newSide3 > newSide1)
                side1 = newSide1;

    side2 = newSide2;
    side3 = newSide3;

This is why it's VERY important to use {...} , for example... 这就是使用{...}非常重要的原因,例如...

public void setSides(double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
    if (newSide1 + newSide2 > newSide3) {
        if (newSide1 + newSide3 > newSide2) {
            if (newSide2 + newSide3 > newSide1) {
                side1 = newSide1;
                side2 = newSide2;
                side3 = newSide3;
            }
        }
    } else {
        throw new IllegalTriangleException(newSide1, newSide2, newSide3);
    }
}

Now, the problem is, the IllegalTriangleException will only be raised when newSide1 + newSide2 > newSide3 , this can be rectified by testing each condition in a single if statement, for example... 现在的问题是,在IllegalTriangleException将只在提出newSide1 + newSide2 > newSide3 ,这可以通过在单个测试每个条件纠正if语句,例如...

public void setSides(double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
    if (newSide1 + newSide2 > newSide3 && 
            newSide1 + newSide3 > newSide2 &&
            newSide2 + newSide3 > newSide1) {
        side1 = newSide1;
        side2 = newSide2;
        side3 = newSide3;
    } else {
        throw new IllegalTriangleException(newSide1, newSide2, newSide3);
    }
}

No offense, but a good IDE should have picked up the issue with the if statements and a debugger would have helped find the other problems... 没有冒犯,但是一个好的IDE应该可以通过if语句解决问题,而调试器可以帮助发现其他问题...

Additional... 额外...

The Triangle constructor is smothering the IllegalTriangleException , this means that it is never thrown to the caller... Triangle构造函数令人窒息IllegalTriangleException ,这意味着它永远不会抛出给调用者...

public Triangle(double side1, double side2, double side3, String color, boolean filled) throws IllegalTriangleException {
    try {
        setSides(side1, side2, side3);(IllegalTriangleException ex) {
        System.out.println("Illegal Triangle: The summ of any two sides must be greater than the other side" + side1 + "," + side2 + "," + side3);
    }
}

Remove the try-catch and let the caller deal with it... 删除try-catch然后让调用者对其进行处理...

public Triangle(double side1, double side2, double side3, String color, boolean filled) throws IllegalTriangleException {
    setSides(side1, side2, side3);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM