Java 没有抛出异常消息

Java 没有抛出异常消息,java,class,exception,coordinates,Java,Class,Exception,Coordinates,嘿,我有一个point类,它生成一个点列表。课程安排如下 public class Point { private double x, y; public Point(double x, double y){ this.x = x; this.y = y; }//end constructor public Point() { // TODO Auto-generated constructor stub } public double getX()

嘿,我有一个point类,它生成一个点列表。课程安排如下

public class Point {

private double x, y;



public Point(double x, double y){

    this.x = x;
    this.y = y;

}//end constructor




public Point() {
    // TODO Auto-generated constructor stub
}

public double getX(){
        return x;
}

public void setX(double x) {
    try{ 
        if (x < 0){
            throw new NegArgumentException();
        }//end if
        else{
            this.x = x;

        }//end else
    }//end try
    catch (NegArgumentException e){
        System.out.println("error");
    }//end catch


}

public double getY() {
    return y;
}

public void setY(double y) {
    try{ 
        if (y < 0){
            throw new NegArgumentException();
        }//end if
        else{
            this.y = y;

        }//end else
    }//end try
    catch (NegArgumentException e){
        System.out.println("error");
    }//end catch

}

public String toString(){
    return "p(" + x + "," + y + ")";
}
}
}

我还有另外两个类矩形和圆形

public class Rectangle {
private Point upperLeft = new Point();
private Point lowerRight = new Point();


public Rectangle(Point upperLeft, Point lowerRight){
    this.upperLeft = upperLeft;
    this.lowerRight = lowerRight;

}//end constructor

public Point getUpperLeft() {
    return upperLeft;
}

public void setUpperLeft(Point upperLeft) {
    this.upperLeft = upperLeft;
}

public Point getLowerRight() {
    return lowerRight;
}

public void setLowerRight(Point lowerRight) {
    this.lowerRight = lowerRight;
}

public Point getUpperRight(){
    return new Point(getLowerRight().getX(), getUpperLeft().getY());
}

public Point getLowerLeft(){
    return new Point(getUpperLeft().getX(), getLowerRight().getY());
}


public double getArea(){
    return upperLeft.getX()*lowerRight.getY();
}

public String toString(){
    return "r[("+upperLeft.getX() + ","+upperLeft.getY()+"),("+lowerRight.getX()+","+lowerRight.getY()+")]";
}
}
还有圆圈班

public class Circle {
private Point center = new Point();
private double radius;

public Circle(){}

public Circle(Point center, double radius){
    this.center = center;
    this.radius = radius;
}

public Point getCenter() {
    return center;
}

public void setCenter(Point center) {
    this.center = center;
}

public String toString(){
    return "c[("+center.getX()+","+center.getY()+"), "+ radius+"]";
}
}
我的主类为每个对象生成一个随机点列表

import java.util.ArrayList;
导入java.util.Random

公共班机{

public static void main(String[] args) {
    //create arraylist for each class
    ArrayList<Point> list = new ArrayList<Point>();
    ArrayList<Rectangle> list1 = new ArrayList<Rectangle>();
    ArrayList<Circle> list2 = new ArrayList<Circle>();
    //create random generators
    Random Point = new Random();
    Random myGenerator = new Random();
    int i = 0;
    //go through for loop
    while (i < 100){

        int whichArray = myGenerator.nextInt(4) + 1;
        //print out points for whichever random object
        if (whichArray == 1){
            int pointX = Point.nextInt(41) - 20;
            int pointY = Point.nextInt(41) - 20;
            if (pointX >= 0 && pointY >= 0){
            list.add(new Point(pointX, pointY));
            System.out.println(list.toString());
            list.remove(0);
            i++;
            }
        }//end if
        if (whichArray == 2){
            int pointX = Point.nextInt(41) - 20;
            int pointY = Point.nextInt(41) - 20;
            int pointRandom = Point.nextInt(20) - 20;
            if (pointX >= 0 && pointY >= 0 && pointRandom >= 0){
            list1.add(new Rectangle(new Point(pointX, pointY), new Point(pointX+pointRandom, pointY-pointRandom)));
            System.out.println(list1.toString());
            list1.remove(0);
            i++;
            }

        }//end if
        if (whichArray == 3){
            int pointX = Point.nextInt(41) - 20;
            int pointY = Point.nextInt(41) - 20;
            int radius = Point.nextInt(41) - 20;
            if (pointX >= 0 && pointY >= 0){
            list2.add(new Circle(new Point(pointX, pointY),radius));
            System.out.println(list2.toString());
            list2.remove(0);
            i++;
            }
        }//end if

    }//end while loop

}//end main


}//end class
publicstaticvoidmain(字符串[]args){
//为每个类创建arraylist
ArrayList=新建ArrayList();
ArrayList list1=新的ArrayList();
ArrayList list2=新的ArrayList();
//创建随机生成器
随机点=新随机点();
Random myGenerator=新的Random();
int i=0;
//循环
而(i<100){
int whichArray=myGenerator.nextInt(4)+1;
//打印任意随机对象的点
如果(whichArray==1){
int pointX=Point.nextInt(41)-20;
int pointY=Point.nextInt(41)-20;
如果(点X>=0&&pointY>=0){
添加(新点(点x,点y));
System.out.println(list.toString());
列表。删除(0);
i++;
}
}//如果结束
如果(whichArray==2){
int pointX=Point.nextInt(41)-20;
int pointY=Point.nextInt(41)-20;
int pointRandom=Point.nextInt(20)-20;
如果(pointX>=0&&pointY>=0&&pointRandom>=0){
列表1.add(新矩形(新点(pointX,pointY)),新点(pointX+pointRandom,pointY-pointRandom));
System.out.println(list1.toString());
列表1.删除(0);
i++;
}
}//如果结束
如果(whichArray==3){
int pointX=Point.nextInt(41)-20;
int pointY=Point.nextInt(41)-20;
int半径=下一点(41)-20;
如果(点X>=0&&pointY>=0){
列表2.添加(新圆(新点(点X、点Y、半径));
System.out.println(list2.toString());
清单2.删除(0);
i++;
}
}//如果结束
}//边结束边循环
}//端干管
}//末级

问题是,我没有看到异常发生时显示的任何消息。我知道这是有效的,因为我的点没有负坐标。有人知道为什么没有显示消息吗?

您的代码中有很多错误做法,您应该阅读一些关于编写干净代码的内容。顺便说一下,不会引发异常,因为从未调用setter。将构造函数更改为

public Point(double x, double y) {    
    setX(x);
    setY(y);   
}
另外,在创建点之前,您始终确保
if
语句的坐标为正,因此无法实例化无效的


最后,即使它与您的“问题”无关,也不要给变量(您的
Random
对象)起一个以大写字母开头的名称,这对于可读性来说是一种不好的做法,如果它也是类的名称,则更糟糕<如果类
Point
和类
Random

中都存在
method
方法,则code>Point.method()
可能不明确。您正在构造函数内设置
Point
的值:

list.add(new Point(pointX, pointY));
所以,永远不会调用setX和setY,所以永远不会验证x和y是否为负数

试着这样做:

public class Main {
public static void main(String[] args) {
    //create arraylist for each class
    ArrayList<Point> list = new ArrayList<Point>();
    ArrayList<Rectangle> list1 = new ArrayList<Rectangle>();
    ArrayList<Circle> list2 = new ArrayList<Circle>();
    //create random generators
    Random Point = new Random();
    Random myGenerator = new Random();
    int i = 0;
    //go through for loop
    while (i < 100){
    int whichArray = myGenerator.nextInt(4) + 1;
    //print out points for whichever random object
    if (whichArray == 1){
        int pointX = Point.nextInt(41) - 20;
        int pointY = Point.nextInt(41) - 20;
        if (pointX >= 0 && pointY >= 0){
            Point point = new Point();
            point.setX(pointX);
            point.setY(pointY);
            list.add(point);
            System.out.println(list.toString());
            list.remove(0);
            i++;
        }
    }//end if
    if (whichArray == 2){
        int pointX = Point.nextInt(41) - 20;
        int pointY = Point.nextInt(41) - 20;
        int pointRandom = Point.nextInt(20) - 20;
        if (pointX >= 0 && pointY >= 0 && pointRandom >= 0){
            Point point = new Point();
            point.setX(pointX);
            point.setY(pointY);

            Point point2 = new Point();
            point2.setX(pointX+pointRandom);
            point2.setY(pointY-pointRandom);

            list1.add(new Rectangle(point, point2));
            System.out.println(list1.toString());
            list1.remove(0);
            i++;
        }

    }//end if
    if (whichArray == 3){
        int pointX = Point.nextInt(41) - 20;
        int pointY = Point.nextInt(41) - 20;
        int radius = Point.nextInt(41) - 20;
        if (pointX >= 0 && pointY >= 0){
        list2.add(new Circle(new Point(pointX, pointY),radius));
        System.out.println(list2.toString());
        list2.remove(0);
        i++;
        }
    }//end if

}//end while loop

}//end main


}//end class
它将打印:

error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(17.0,10.0), 13.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(20.0,6.0), 5.0]]
[c[(18.0,1.0), -18.0]]
[c[(18.0,20.0), 10.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(20.0,19.0), 20.0]]
[c[(20.0,12.0), -14.0]]
[c[(2.0,20.0), 0.0]]
error
error
[p(0.0,0.0)]
[c[(10.0,13.0), 2.0]]
[c[(10.0,4.0), 15.0]]
[c[(20.0,7.0), 16.0]]
[c[(14.0,18.0), 2.0]]
[c[(20.0,8.0), 17.0]]
[c[(16.0,15.0), -13.0]]
[c[(7.0,6.0), 1.0]]
[c[(11.0,0.0), -15.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(15.0,2.0), 20.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(3.0,5.0), -7.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(16.0,5.0), -10.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(9.0,1.0), -3.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(13.0,17.0), 7.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(7.0,16.0), 14.0]]
error
error
[p(0.0,0.0)]
[c[(17.0,13.0), -5.0]]
[c[(14.0,8.0), 6.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(13.0,0.0), 14.0]]
[c[(1.0,9.0), 2.0]]
error
error
[p(0.0,0.0)]
[c[(2.0,17.0), -1.0]]
[c[(17.0,8.0), -6.0]]
error
error
[p(0.0,0.0)]
[c[(7.0,19.0), -10.0]]
[c[(20.0,7.0), 3.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(2.0,7.0), -10.0]]
error
error
[p(0.0,0.0)]
[c[(2.0,9.0), 14.0]]
error
error
[p(0.0,0.0)]
[c[(5.0,15.0), 18.0]]
error
error
[p(0.0,0.0)]
[c[(18.0,18.0), -5.0]]
[c[(6.0,2.0), -15.0]]
[c[(12.0,4.0), -3.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(16.0,0.0), 20.0]]
error
error
[p(0.0,0.0)]
[c[(11.0,12.0), -19.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(2.0,13.0), 13.0]]
[c[(20.0,2.0), 11.0]]
[c[(0.0,6.0), -10.0]]
error
error
[p(0.0,0.0)]
[c[(17.0,17.0), 7.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(0.0,16.0), -11.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(14.0,4.0), -2.0]]
[c[(0.0,3.0), 11.0]]
[c[(11.0,15.0), -7.0]]
[c[(19.0,16.0), 19.0]]
[c[(8.0,13.0), 15.0]]

编辑问题:问题是我不能用0代替负坐标。我需要一个正数

将点类方法更改为:

public void setX(double x) {
    try{ 
        if (x <= 0){
            throw new NegArgumentException();
        }//end if
        else{
            this.x = x;

        }//end else
    }//end try
    catch (NegArgumentException e){
        System.out.println("error");
    }//end catch

}

当它为0时,我将继续生成另一个随机数。

何时以及为什么会发生异常?我看到
如果(pointX>=0&&pointY>=0)
到处都有守卫!问题是当我把这些都处理掉,任何本来是负数的数字都会变为0,所以我的随机坐标的0比它应该的多@ElliottFrisch为什么不只是你想开始的范围?我希望,但我的赋值特别声明,当我执行你的方法时,捕捉出现在@ElliottFrisch的任何负数,它告诉我,该方法对于类型是未定义的随机的当我试着施法时,它说不能施法。问题是我不能用0来代替负坐标。我需要得到一个正数。再次为您编辑。我非常感谢您的帮助,但我仍然得到一个异常数量的0。我认为问题可能在于,对于随机生成器吐出的每个负数,它都将这些坐标初始化为0。我需要它来重新生成一个新的随机坐标,直到我得到一个正数。好的,我现在明白了,我读了random.nextInt的规范,它说“返回一个伪随机的,均匀分布的int值,介于0(包含)和指定值(独占)”之间,所以0可能发生,我会尽力帮你解决这个问题
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(17.0,10.0), 13.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(20.0,6.0), 5.0]]
[c[(18.0,1.0), -18.0]]
[c[(18.0,20.0), 10.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(20.0,19.0), 20.0]]
[c[(20.0,12.0), -14.0]]
[c[(2.0,20.0), 0.0]]
error
error
[p(0.0,0.0)]
[c[(10.0,13.0), 2.0]]
[c[(10.0,4.0), 15.0]]
[c[(20.0,7.0), 16.0]]
[c[(14.0,18.0), 2.0]]
[c[(20.0,8.0), 17.0]]
[c[(16.0,15.0), -13.0]]
[c[(7.0,6.0), 1.0]]
[c[(11.0,0.0), -15.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(15.0,2.0), 20.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(3.0,5.0), -7.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(16.0,5.0), -10.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(9.0,1.0), -3.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(13.0,17.0), 7.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(7.0,16.0), 14.0]]
error
error
[p(0.0,0.0)]
[c[(17.0,13.0), -5.0]]
[c[(14.0,8.0), 6.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(13.0,0.0), 14.0]]
[c[(1.0,9.0), 2.0]]
error
error
[p(0.0,0.0)]
[c[(2.0,17.0), -1.0]]
[c[(17.0,8.0), -6.0]]
error
error
[p(0.0,0.0)]
[c[(7.0,19.0), -10.0]]
[c[(20.0,7.0), 3.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(2.0,7.0), -10.0]]
error
error
[p(0.0,0.0)]
[c[(2.0,9.0), 14.0]]
error
error
[p(0.0,0.0)]
[c[(5.0,15.0), 18.0]]
error
error
[p(0.0,0.0)]
[c[(18.0,18.0), -5.0]]
[c[(6.0,2.0), -15.0]]
[c[(12.0,4.0), -3.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(16.0,0.0), 20.0]]
error
error
[p(0.0,0.0)]
[c[(11.0,12.0), -19.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(2.0,13.0), 13.0]]
[c[(20.0,2.0), 11.0]]
[c[(0.0,6.0), -10.0]]
error
error
[p(0.0,0.0)]
[c[(17.0,17.0), 7.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(0.0,16.0), -11.0]]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
error
error
[p(0.0,0.0)]
[c[(14.0,4.0), -2.0]]
[c[(0.0,3.0), 11.0]]
[c[(11.0,15.0), -7.0]]
[c[(19.0,16.0), 19.0]]
[c[(8.0,13.0), 15.0]]
public void setX(double x) {
    try{ 
        if (x <= 0){
            throw new NegArgumentException();
        }//end if
        else{
            this.x = x;

        }//end else
    }//end try
    catch (NegArgumentException e){
        System.out.println("error");
    }//end catch

}
public class Main {
public static void main(String[] args) {
    //create arraylist for each class
    ArrayList<Point> list = new ArrayList<Point>();
    ArrayList<Rectangle> list1 = new ArrayList<Rectangle>();
    ArrayList<Circle> list2 = new ArrayList<Circle>();
    //create random generators
    Random randomNum = new Random();
    Random myGenerator = new Random();
    int i = 0;
    //go through for loop
    while (i < 100){

        int whichArray = myGenerator.nextInt(4) + 1;
        //print out points for whichever random object
        if (whichArray == 1){
            int pointX = 0;
            while (pointX == 0){
                pointX= randomNum.nextInt(41) - 20;
            }
            int pointY = 0;
            while(pointY == 0){
                pointY = randomNum.nextInt(41) - 20;
            }
            if (pointX >= 0 && pointY >= 0){
                Point point = new Point();
                point.setX(pointX);
                point.setY(pointY);
                list.add(point);
                System.out.println(list.toString());
                list.remove(0);
                i++;
            }
        }//end if
        if (whichArray == 2){
            int pointX = 0;
            while (pointX == 0){
                pointX= randomNum.nextInt(41) - 20;
            }
            int pointY = 0;
            while(pointY == 0){
                pointY = randomNum.nextInt(41) - 20;
            }
            int pointRandom = randomNum.nextInt(20) - 20;
            if (pointX >= 0 && pointY >= 0 && pointRandom >= 0){
                Point point = new Point();
                point.setX(pointX);
                point.setY(pointY);

                Point point2 = new Point();
                point2.setX(pointX+pointRandom);
                point2.setY(pointY-pointRandom);

                list1.add(new Rectangle(point, point2));
                System.out.println(list1.toString());
                list1.remove(0);
                i++;
            }

        }//end if
        if (whichArray == 3){
            int pointX = 0;
            while (pointX == 0){
                pointX= randomNum.nextInt(41) - 20;
            }
            int pointY = 0;
            while(pointY == 0){
                pointY = randomNum.nextInt(41) - 20;
            }
            int radius = randomNum.nextInt(41) - 20;
            if (pointX >= 0 && pointY >= 0){
            list2.add(new Circle(new Point(pointX, pointY),radius));
            System.out.println(list2.toString());
            list2.remove(0);
            i++;
            }
        }//end if

    }//end while loop

}//end main


}//end class