Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试捕捉范围问题_Java_Try Catch - Fatal编程技术网

Java 尝试捕捉范围问题

Java 尝试捕捉范围问题,java,try-catch,Java,Try Catch,在处理try-and-catch时,我很难确定什么应该放在什么范围内,我的部分代码要求用户键入他们想要扫描的文件名,这是try-and-catch语句: try{ System.out.println("Enter the name of the file you wouldlike to scan: "); String fileName = scan.nextLine(); File file = new File(fileName); BufferedRe

在处理try-and-catch时,我很难确定什么应该放在什么范围内,我的部分代码要求用户键入他们想要扫描的文件名,这是try-and-catch语句:

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 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
 }
当然,这也适用于可能导致异常的任何其他对象,并且在程序中的许多位置都需要这些对象。

BufferedReader的作用域将限于try块。您应该将对BufferedReader上的方法的所有调用放置在此块中。

catch块与try块相关联。因此,您的代码将是

try {
    // read file
    //.....
} catch(IOException e) {
    System.out.println(e);
    System.exit(-1);
}
更多阅读:


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

try块中声明的变量的作用域以其终止结束。因此,您案例中的变量“br”在try块之外无法识别

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

使用相应的变量获取所需的相应详细信息

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


希望这有帮助。

缺少一个;在新的BufferedReader行的末尾,能否将try语句中的代码发布到line=br.readLine@SameerSawla在第一个发布的代码中有语法错误。我已经指出了这一部分。还有一些语义错误,你还没有解决。还有一个实际的问题,你还没有回答-1.