如何为此代码添加throws Exception?

时间:2014-05-11 04:06:37

标签: file ioexception filewriter printwriter throws

这是我的主要课程:

public class Table extends java.applet.Applet implements Runnable
{
    public void init()
    {
        Balla.addBall();
    }
}

这是Balla方法:

public static void addBall()throws IOException
    {
        Random generator = new Random();
        Ball b = new Ball(100,100,8,Color.blue,generator.nextInt(4)+1,generator.nextInt(4)+1);
        FileWriter fw = new FileWriter("C:\\temp_Jon\\BallData.ballz",true);
        PrintWriter pw = new PrintWriter(fw,true);
        pw.print(b.getX()+" "+b.getY()+" "+b.getRadius()+" "+b.color+" "+b.speedX+" "+b.speedY);
        fw.close();
        pw.close();
    }

现在我的问题来自于我编写它的时候。它告诉我在init方法中有一个未报告的IOException,但是当我在方法上添加抛出IOException时它会告诉我:
 错误:表中的init()不能覆盖Applet中的init()

我如何解决这个问题和/或如何在不修改所有内容的情况下解决这个问题?

1 个答案:

答案 0 :(得分:0)

更改此

public static void addBall()throws IOException
    {
        Random generator = new Random();
        Ball b = new Ball(100,100,8,Color.blue,generator.nextInt(4)+1,generator.nextInt(4)+1);
        FileWriter fw = new FileWriter("C:\\temp_Jon\\BallData.ballz",true);
        PrintWriter pw = new PrintWriter(fw,true);
        pw.print(b.getX()+" "+b.getY()+" "+b.getRadius()+" "+b.color+" "+b.speedX+" "+b.speedY);
        fw.close();
        pw.close();
    }

public static void addBall()
    {
        Random generator = new Random();
        Ball b = new Ball(100,100,8,Color.blue,generator.nextInt(4)+1,generator.nextInt(4)+1);
        try
{
        FileWriter fw = new FileWriter("C:\\temp_Jon\\BallData.ballz",true);
        PrintWriter pw = new PrintWriter(fw,true);
        pw.print(b.getX()+" "+b.getY()+" "+b.getRadius()+" "+b.color+" "+b.speedX+" "+b.speedY);
        fw.close();
        pw.close();
    }
catch(Exception e)
{
}
}

看看它是否有效