创建对象时的Java.lang.exception

时间:2014-10-23 11:07:33

标签: java

我正在尝试进行一项任务,我必须检查矩形是否重叠以及其他一些方法。我几乎完成了它但我无法从类中创建新对象。它给了我 错误:(121,32)java:unreported exception java.lang.Exception;必须被抓住或宣布被抛出这是我的代码

  /**
 * Created by Sarang on 18-10-2014.
 */
import java.awt.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.io.*;


public class Rectangle {
    private int upper_x, upper_y, lower_x, lower_y;

    public Rectangle(int upper1, int upper2,int lower1,int lower2) throws Exception {
        this.upper_x=upper1;
        this.upper_y=upper2;
        this.lower_x=lower1;
        this.lower_y=lower2;
        try {
            if (upper_x > lower_x || upper_y > lower_y)
            {
                throw new IllegalArgumentException();   //Checking if the rectangle is valid
            }
            if(lower_x  >500)
            {lower_x=500;}
            if(lower_y>500)
            {lower_y=500;}  //Setting the lower bounds to 500
            if(upper_x<50)
            {upper_x=50;}
            if(upper_y<50)
            {upper_y=50;}
        } catch (IllegalArgumentException IAE) {
                System.out.println("Invalid Rectangle");
        }
    }
    Rectangle( Rectangle other)
    {
        upper_x=other.upper_x;
        upper_y=other.upper_y;
        lower_x=other.lower_x;
        lower_y=other.lower_y;
    }
    public boolean overlap(Rectangle other)
    {
        if(upper_x<other.upper_x && upper_y<other.upper_y && lower_x>other.lower_x && lower_y>other.lower_y)
              { return false;}
        if (!( lower_y < other.upper_y || upper_y > other.lower_y || lower_x < other.upper_x || upper_x > other.lower_x ))
              { return true;}
        return false;
    }
    public boolean containedIn(Rectangle other)
    {
        if(upper_x<other.upper_x && upper_y<other.upper_y && lower_x>other.lower_x && lower_y>other.lower_y)
            { return true;}
        if(other.upper_x<upper_x && other.upper_y<upper_y && other.lower_x>lower_x && other.lower_y>lower_y)
        {
            return true;
        }
        return false;
    }
    public boolean drag(int x, int y)
    {
        int temp_1x=upper_x;
        int temp_1y=upper_y;
        int temp_2x=lower_x;
        int temp_2y=lower_y;
        int length=lower_x-upper_x;
        int height=lower_y-upper_y;
        upper_x=x-length/2;
        upper_y=y-height/2;
        lower_x=x+length/2;
        lower_x=y+height/2;
        if(upper_x<50||upper_y<50||lower_x>500||lower_y>500)
        {
            upper_x=temp_1x;
            upper_y=temp_1y;
            lower_x=temp_2x;
            lower_y=temp_2y;
            return false;
        }
        return true;

    }

    public boolean resize(int x, int y)
    {
        int temp_x,temp_y;
        temp_x=lower_x;
        temp_y=lower_y;

        lower_x=x;
        lower_y=y;
        if (upper_x > lower_x || upper_y > lower_y)
        {
            lower_x=temp_x;
            lower_y=temp_y;
            return false;
        }
        if(lower_x  >500||lower_y>500)
        {
            lower_x=temp_x;
            lower_y=temp_y;
            return false;
        }
        return true;
    }
    public int getUpperX(){return upper_x;}
    public int getUpperY(){return upper_y;}
    public int getLowerX(){return lower_x;}
    public int getLowerY(){return lower_y;}



    public static void main(String[] args) throws IOException {
        Rectangle rectangle1 = new Rectangle(60,70,400,400);
        Rectangle rectangle2 = new Rectangle(100,100,450,450);
        System.out.println("rectangle1.overlap(rectangle2):"  + rectangle1.overlap(rectangle2));
        System.out.println("rectangle1.containedIn(rectangle2):" +rectangle1.containedIn(rectangle2));

    }


    /*
     *
     * The code below this comment is for visual display of a 2D array of Rectangles.
     * You are not supposed to make any changes or add any code below this comment.
     *
     */
    class showRecs extends JFrame {
        public showRecs(Rectangle[] rectArr) {
//         super("Display Arrays");
//         Rectangle[] r1 = rt.rArr;
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            displayRecs recs = new displayRecs();
            getContentPane().add(recs);
            for (int i = 0; i < rectArr.length; i++) {
                int w = rectArr[i].getLowerX() - rectArr[i].getUpperX();
                int h = rectArr[i].getLowerY() - rectArr[i].getUpperY();
                recs.addArrays(rectArr[i].getUpperX(), rectArr[i].getUpperY(), w, h);
            }

            pack();
            setLocationRelativeTo(null);
            setVisible(true);

        }

    }

    class displayRecs extends JPanel {
        private static final int frame_width = 500;
        private static final int frame_height = frame_width;
        private List<java.awt.Rectangle> rects = new ArrayList<java.awt.Rectangle>();

        public void addArrays(int x, int y, int width, int height) {
            java.awt.Rectangle rect = new java.awt.Rectangle(x, y, width, height);
            rects.add(rect);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(frame_width, frame_height);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            for (java.awt.Rectangle rect : rects) {
                Random rand = new Random();
                int R = rand.nextInt(255);
                int G = rand.nextInt(255);
                int B = rand.nextInt(255);

                Color color = new Color(R, G, B);
                g.setColor(color);
                g.drawString("(" + rect.x + "," + rect.y + ")", rect.x - 45, rect.y);
                int m = rect.x + rect.width;
                int l = rect.y + rect.height;
                int idx = rects.indexOf(rect);
                g.drawString("(" + m + "," + l + ")", m + 1, l + 1);
                int locx = rect.x + rect.width / 2;
                int locy = rect.y + rect.height / 2;
                g.drawString((String.valueOf(idx)), locx, locy);
                g2.draw(rect);
            }
        }

    }
}

我在代码中做错了什么。我还没有应用其他一些方法,但它没有在这个阶段运行

4 个答案:

答案 0 :(得分:1)

当您的构造函数抛出异常时,您只能在捕获new的try / catch块中的Rectangle类上调用Exception

答案 1 :(得分:1)

你有:

public Rectangle(int upper1, int upper2,int lower1,int lower2) throws Exception {
                                                               ^^^^^^^^^^^^^^^^^

这就是说你的Rectangle构造函数可以抛出任何类型的异常。这意味着任何调用构造函数的代码都必须处理任何类型的异常(通过捕获它或将其声明为抛出)。

看起来您的构造函数实际上只是抛出IllegalArgumentException,这是未选中的(不需要在throws子句中声明)。因此,如果您取消throws,它应该有用。

public Rectangle(int upper1, int upper2,int lower1,int lower2) {
    ...

答案 2 :(得分:1)

我们的基本问题是你已经宣布矩形的构造函数可以抛出一个异常(它没有这样做,所以这个声明很奇怪)。

在你的主程序中你不能实例化对象,因为这会调用构造函数 - 现在编译器知道构造函数可能抛出异常(因为你这样声明)所以它不会让你实例化实例你没有试一试 - 围绕实例化。

总而言之,我并不认为你想要抛出...构造函数声明的一部分 - 如果你为某些(未来)的原因需要这个构造函数抛出然后你必须进行适当的尝试 - 抓住声明来抓住潜在的投掷。

答案 3 :(得分:0)

你的构造函数如下所示:

public Rectangle(int upper1, int upper2,int lower1,int lower2) throws Exception

throws Exception表示它可能会抛出一个异常,这意味着调用者(这里是你的主要方法)需要try-catch它。