Unreported exception java.io.IOException when compiling Java code

后端 未结 2 353
余生分开走
余生分开走 2021-01-26 01:36

I am experiencing a problem with this code:

public class gravityv1 {
    public static double[] calculategravity(double[] mass, int[] diameter) {
        double[         


        
相关标签:
2条回答
  • 2021-01-26 02:21

    You need a try-catch block in your main method around all methods that throw an IOException:

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

    Include all methods that have throws IOException after the signature.

    0 讨论(0)
  • 2021-01-26 02:25

    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
    which B can throw (unless they are descendants of RuntimeException or unless A
    is catching and processing them explicitly in a try-catch block).

    In your case A is main, B is printFile.

    0 讨论(0)
提交回复
热议问题