簡體   English   中英

嘗試捕捉范圍問題

[英]Try and catch scope problems

我在嘗試使用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

try block聲明的變量的范圍以其終止結束。 因此,在try塊之外無法識別您所用的變量'br'

我的建議是,在try塊外部聲明相應的變量。 檢查是否在try block引發FileNotFoundException 成功退出try catch block

使用相應的變量來獲取所需的相應詳細信息。

這是背后的理論邏輯。 Edgar Boda正確顯示了正確的編碼機制,以使您獲得正確的解決方案。 閱讀兩個答案將有助於您理解為什么首先遇到此問題。

希望這可以幫助。

catch塊與try塊相關聯。 所以你的代碼將是

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

更多閱讀: Oracle教程

br移出try塊作用域。 編譯器抱怨無法識別br因為在try塊作用域后無法看到br Java范圍

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM