【问题标题】:how to access Variable outside a try catch block如何在 try catch 块之外访问变量
【发布时间】:2013-02-08 10:34:02
【问题描述】:

我正在尝试从具有 try-catch 块的函数中返回一个布尔值

但问题是我无法返回任何值。

我知道 try-catch 块中的变量无法在其外部访问,但我仍然想访问。

public boolean checkStatus(){
        try{


        InputStream fstream = MyRegDb.class.getClassLoader().getResourceAsStream("textfile.txt");
        // Use DataInputStream to read binary NOT text.
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;

        //Read File Line By Line
        strLine = br.readLine();
        // Print the content on the console
        System.out.println (strLine);

        ind.close();
        if(strLine.equals("1")){

            return false;   
        }else{
            return true;    
        }

    }catch(Exception e){}
}   

在我的项目中,这对我来说是一个非常严重的问题。 我用谷歌搜索,并尝试了自己,但没有解决。

我希望现在我能找到一些解决方案。 我知道它有错误说 return statement missing,但我希望程序完全像这样工作。

现在我对此很严格的原因

在我的 jar 文件中,我必须访问文本文件以查找值 1 或 0,如果“1”则激活,否则停用。

这就是我使用布尔值的原因。

【问题讨论】:

    标签: java return try-catch return-value


    【解决方案1】:
    import java.io.*;
    public class GameHelper 
    {
        public String getUserInput(String prompt) {
            String inputLine = null;
            System.out.print(prompt + “ “);
            try {
                BufferedReader is = new BufferedReader(
                new InputStreamReader(System.in));
                inputLine = is.readLine();
                if (inputLine.length() == 0 ) return null;
            } 
            catch (IOException e) {
                System.out.println(“IOException: “ + e);
            }
            return inputLine;
        }
    }
    

    确保在编写程序之前编写import java.io.*。现在您甚至可以在trycatch 之外返回函数

    【讨论】:

      【解决方案2】:

      错误是在抛出异常的情况下您没有返回任何内容。

      尝试以下方法:

      public boolean checkStatus(){
         boolean result = true;  // default value.
         try{
      
              InputStream fstream = MyRegDb.class.getClassLoader().getResourceAsStream("textfile.txt");
              // Use DataInputStream to read binary NOT text.
              BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
              String strLine;
      
              //Read File Line By Line
              strLine = br.readLine();
              // Print the content on the console
              System.out.println (strLine);
      
              ind.close();
              if(strLine.equals("1")){
      
                  result = false;   
              }else{
                  result = true;    
              }
      
          }catch(Exception e){}
          return result;
      }  
      

      【讨论】:

        【解决方案3】:

        在 try/catch 块之前声明字符串 strLine
        然后在 try/catch 块之后写你的 if 语句,如

            String strLine;
        
            try{
                //......
            }catch(Exception e){
                //.....
            }
        
            if(strLine.equals("1"))
               return false;   
        
            return true;    
        

        摆脱 else 块。

        【讨论】:

          【解决方案4】:

          在您的方法中,如果抛出Exception,则没有返回语句。将return 语句放在异常处理程序中、finally 块中或异常处理程序之后。

          【讨论】:

            【解决方案5】:

            只需在 try/catch 之外声明布尔值,并在 try 块中设置值

            public boolean myMethod() {
                boolean success = false;
                try {
                    doSomethingThatMightThrowAnException();
                    success = true;
                }
                catch ( Exception e ) {
                    e.printStackTrace();
                }
                return success;
            }
            

            【讨论】:

              猜你喜欢
              • 2018-08-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-11-02
              • 1970-01-01
              • 2014-06-23
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多