【问题标题】:Try and catch scope problems尝试捕获范围问题
【发布时间】:2013-12-02 05:56:56
【问题描述】:

我无法确定在使用 try 和 catch 处理时应该将什么放置在什么范围内,我的部分代码要求用户输入他们想要扫描的文件名,这是 try 和捕获语句:

try{
    System.out.println("Enter the name of the file you wouldlike to scan: ");
    String fileName = scan.nextLine();

    File file = new File(fileName);
    BufferedReader br = new BufferedReader(new FileReader(fileName)                        }                   
    catch( IOException ioe){
                                System.out.println(ioe);
                                System.exit(-1);
                        }

当我编译它在“line = br.readLine();”中找不到符号“br” //扫描文件的部分代码,不知道在try语句的哪个作用域里放什么

这是代码的另一个(不是整个程序)部分,我用 system.in 测试了这部分并且工作正常,但不能与 filereader 一起工作

String line;
            int lineCount = 0, wordCount = 0, charCount = 0, palCount = 0;
            int thisLineWords = 0, thisLineChars = 0;
            boolean isPal = true;

            do {
                    try {
                            line = br.readLine();
                    }
                    catch( Exception e ) {
                            break;
                    }

                    if( line == null ) break;


                    if( line.compareTo(".") == 0 ) break;

                    thisLineWords = 0;
                    thisLineChars = line.length() + 1; // count chars
                    isPal = true;

                    //count words
                    boolean inWord = false;
                    for( int i = 0; i < line.length(); i++ ) {
                            char ch = line.charAt(i);
                            if( Character.isWhitespace(ch) ) {
                                    if( inWord ) inWord = false;
                            }
                            else {
                                    if( !inWord ) {
                                            inWord = true;
                                            thisLineWords++;
                                    }
                            }
                    }

【问题讨论】:

  • 缺少一个 );在新的 BufferedReader 行的末尾
  • 您可以将try语句中的代码发布到line = br.readLine();吗?

标签: java try-catch


【解决方案1】:

当你有这样的东西时,我通常会做以下事情:

 BufferedReader br = null; // here declare the object you want later to use
 try {
      // now the part that could cause the exception
      br = new BufferedReader(new FileReader(fileName)); 
 } catch (IOException ioe) {
      // handle exception
 }


 if (br != null) {
    // now use br outside of try/catch block
 }

当然,这也适用于任何其他可能导致异常并且在程序中的许多地方都需要的对象。

【讨论】:

    【解决方案2】:

    BufferedReader 的范围将限于 try 块。您应该将所有对方法的调用放在此块内的 BufferedReader 上。

    【讨论】:

      【解决方案3】:

      try block 中声明的变量的范围以它的终止结束。因此,在您的情况下,变量 'br' 在 try 块之外未被识别。

      我的建议是,在 try 块之外声明相应的变量。检查它是否在try block 中抛出FileNotFoundException。当成功退出try catch block

      使用相应的变量来获取您需要的相应详细信息。

      这就是它背后的理论逻辑。 Edgar Boda 正确地展示了正确的编码机制,让您找到正确的解决方案。阅读这两个答案将有助于您了解为什么会首先遇到此问题。

      希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        catch 块与try 块相关联。所以你的代码将是

        try {
            // read file
            //.....
        } catch(IOException e) {
            System.out.println(e);
            System.exit(-1);
        }
        

        更多阅读:Oracle tutorial

        br 移出try 块范围。编译器抱怨无法识别br,因为在尝试块范围br 后看不到。 Java Scope

        【讨论】:

        • @SameerSawla 在第一个发布的代码中存在语法错误。我已经指出了那部分。
        • 还有一些你没有解决的语义错误。还有一个实际的问题,你还没有回答。 -1
        猜你喜欢
        • 1970-01-01
        • 2022-11-14
        • 1970-01-01
        • 1970-01-01
        • 2017-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多