简体   繁体   English

编译Java代码时出现未报告的异常java.io.IOException

[英]Unreported exception java.io.IOException when compiling Java code

I am experiencing a problem with this code: 我遇到此代码的问题:

public class gravityv1 {
    public static double[] calculategravity(double[] mass, int[] diameter) {
        double[] gravity = new double[mass.length];

        for (int n = 0; n < mass.length; n++) {
            gravity[n] = (6.67E-11 * mass[n]) / Math.pow((diameter[n] / 2), 2);

        }

        return gravity;
    }

    public static void print(double[] gravity, double[] mass, int[] diameter, String[] planet) {
        System.out.println("                          Planetary Data");
        System.out.println("Planet          Diameter(km)          Mass(kg)          g(m/s^2)");
        System.out.println("---------------------------------------------------------------------");
        for (int n = 0; n < planet.length; n++)
        System.out.printf("%-7s%15d%20.3f%14.2f", planet[n], diameter[n], mass[n], gravity[n]);
    }

    public static void printFile(double[] gravity) throws IOException {
        PrintWriter outFile = new PrintWriter(new File("gravity.txt"));
        for (double gravita: gravity) {
            outFile.println(gravita);
        }
        outFile.close();
    }

    public static void main(String[] args) {
        String[] planet = {
            "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"
        };
        double[] mass = {.330E24, 4.87E24, 5.97E24, 0.642E24, 1898E24, 568E24, 86.8E24, 102E24
        };
        int[] diameter = {
            4879, 12104, 12756, 6792, 142984, 120536, 51118, 49528
        };

        double[] gravity = calculategravity(mass, diameter);

        print(gravity, mass, diameter, planet);
        printFile(gravity);
    }
}

Whenever I try to compile this code, an error appears at " printFile(gravity) ", which is located at the bottom. 每当我尝试编译此代码时, printFile(gravity)在底部的“ printFile(gravity) ”处出现错误。 The error message is: 错误消息是:

unreported exception java.io.IOException; 未报告的异常java.io.IOException; must be caught or declared to be thrown. 必须被抓住或宣布被抛出。

I don't know what to do. 我不知道该怎么办。

Change your main method declaration to: 将您的主要方法声明更改为:

public static void main(String[] args) throws IOException

In Java, each method A calling another method B, must declare all the exceptions 在Java中,每个方法A调用另一个方法B都必须声明所有异常
which B can throw (unless they are descendants of RuntimeException or unless A B可以抛出的RuntimeException (除非它们是RuntimeException后代,或者除非A
is catching and processing them explicitly in a try-catch block). 在try-catch块中显式捕获和处理它们)。

In your case A is main , B is printFile . 在您的情况下,A是main ,B是printFile

You need a try-catch block in your main method around all methods that throw an IOException : 在所有引发IOException方法的main方法中,您需要一个try-catch块:

try {
    printFile(gravity);
} catch (IOException ex) {
    // handle exception here
}

Include all methods that have throws IOException after the signature. 包括在签名后throws IOException所有方法。

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

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