Java 如何访问try-catch块外部的变量

Java 如何访问try-catch块外部的变量,java,return,try-catch,return-value,Java,Return,Try Catch,Return Value,我正在尝试从具有try catch block的函数中返回布尔值 但问题是我不能返回任何值 我知道try-catch块中的变量不能在其外部访问,但我仍然想访问它 public boolean checkStatus(){ try{ InputStream fstream = MyRegDb.class.getClassLoader().getResourceAsStream("textfile.txt"); // Use DataInputSt

我正在尝试从具有try catch block的函数中返回布尔值

但问题是我不能返回任何值

我知道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){}
}   
在我的项目中,这是一个非常严重的问题。 我在谷歌上搜索了一下,试了试,但什么也没解决

我希望现在我能找到一些解决办法。 我知道返回语句丢失时会出现错误,但我希望程序能像这样工作

现在我对这个要求严格的原因是

在我的jar文件中,我必须访问文本文件以查找值1或0(如果为“1”),然后激活或停用


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

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

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

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

错误在于,在引发异常的情况下,您没有返回任何内容

请尝试以下操作:

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;
}  

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

    String strLine;

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

    if(strLine.equals("1"))
       return false;   

    return true;    
摆脱else块

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.*
。现在,即使在
try
catch