为什么 PMD 会提示我他的扫描仪没有关闭?

Why does PMD signal me that he scanner isn't closed?

所以在下面的代码中,PMD 告诉我扫描仪键盘和扫描未关闭,并带有 [CLoseResource] 警告。我想我关闭了我确保它总是关闭的,所以我不知道是什么导致了这个警告。代码本身运行良好,这些是 PMD 向我抛出的唯一严重警告。

代码位 1:

public static void main(final String... args) {
        Scanner scan = null;
        try {
            if (args[0].equals("-f")) {
                try {
                    scan = new Scanner(new File(args[1]));
                }catch(FileNotFoundException e) {
                    System.out.println(e.getMessage());
                    System.exit(START_ERROR);
                }
            }else {
                System.out.println("Unbekanntes Kommando.");
                System.exit(START_ERROR);
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Nichts eingegeben.");
            System.exit(START_ERROR);
        }
        try {
            validateSize(scan);
        }catch (InvalidBoardLayoutException e) {
            System.out.println(e.getMessage());
            scan.close();
            System.exit(BOARD_ERROR);
        }
        scan.close();
        try {
            scan=new Scanner(new File(args[1]));
        } catch (FileNotFoundException e) { //This should be impossible
            System.out.println("This should be impossible lmao");
            scan.close();
            System.exit(666);
        }
        try {
            validateFields(scan);
        }catch(InvalidFieldException e) {
            System.out.println(e.getMessage());
            scan.close();
            System.exit(BOARD_ERROR);
        }
        scan.close();
        try {
            scan=new Scanner(new File(args[1]));
        } catch (FileNotFoundException e) { //This should be impossible
            System.out.println("This should be impossible lmao");
        }
        final Game game = new Game(scan);
        game.play();
        scan.close();
    }

代码位 2:

        System.out.println("Enter your next move!");
        Scanner keyboard = new Scanner(System.in);
        String move = keyboard.nextLine();
        if (move.isEmpty()) {
            won = true;
            keyboard.close();
            return;
        }
        if (!validateFormat(move)) {
            System.out.println("Invalid format, try again.");
            return;
        }
        String[] moveAr;
        try {
            moveAr = move.split(",");
        } catch (PatternSyntaxException e) { //this should be impossible
            System.out.println(e.getMessage());
            return;
        }
        try{
            validFields(moveAr);
        }catch(InvalidTurnException e){
            System.out.println(e.getMessage());
            return;
        }
        final char colour = getColour(moveAr[0]);
        for (final String s : moveAr) {
            final int line = Character.getNumericValue(s.charAt(1)) - 1;
            final int column = getColumn(s.charAt(0));
            this.spielFeld[line][column] = Character.toUpperCase(this.spielFeld[line][column]);
            final char columnc = s.charAt(0);
            if (columnCrossed(columnc)){
                points += crossedValues(columnc);
            }
        }
        if (colourComplete(colour)){
            points += 5;
            coloursCrossed++;
        }
        if (coloursCrossed >= 2){
            won = true;
            keyboard.close();
        }
        System.out.println("Momentane Punkte: "+points);
    }```

如果try中的代码抛出一个没有被捕捉到的异常,则scanner没有关闭。正如评论中已经提到的,尝试使用资源是处理此问题的正确方法。