Java:标记“catch”上的语法错误,(预期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33457963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:42:05  来源:igfitidea点击:

Java: Syntax error on token "catch", ( expected

javasyntaxtry-catch

提问by user2255429

I keep getting an error in the Eclipse IDE:

我在 Eclipse IDE 中不断收到错误消息:

Syntax error on token "catch", ( expected

I'm not sure where the syntax error is, could someone help please?

我不确定语法错误在哪里,有人可以帮忙吗?

public class ReadCSV {

    private static HashMap <String, String> rankDomainMap; 

    public ReadCSV() {
        rankDomainMap = new HashMap<>();
    }

    public HashMap getHashmap() {
        return rankDomainMap;
    }

    public void run(int id) {

        File top1m = new File("top-1m.csv");

        String line = "";

        int blockSize = 10000;

        try ( FileReader fileReader = new FileReader(top1m); BufferedReader br = new BufferedReader(fileReader) )
            {
                while ( ((line = br.readLine()) != null) ) {
                    String parts[] = line.split(",");
                    for (int count=1; count < blockSize; count++){
                        if ( (Integer.parseInt(parts[0]) >= (id) ) && (Integer.parseInt(parts[0])+blockSize < id) ) {
                                rankDomainMap.put(parts[0], parts[1]);
                        }
                    }
                }

            }


        System.out.println( rankDomainMap.toString() );

        catch (FileNotFoundException e)  {
            e.printStackTrace();
        }
    }
}

回答by the happy mamba

catch has to directly follow the closing brace of the try. Currently the code has a System.out.println.

catch 必须直接跟在 try 的右大括号后面。当前代码有一个 System.out.println。

It needs to look like this:

它需要看起来像这样:

try {
    ......
}
catch(Exception e)
{
   .....
}

回答by SLaks

You're missing a }before the catch.

你错过了}之前的catch