java 如何在try catch块之外访问变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15030438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:17:01  来源:igfitidea点击:

how to access Variable outside a try catch block

javareturntry-catchreturn-value

提问by Ruman Khan Pathan

I'm trying to return a boolean valuefrom my function having try-catch block,

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

But the problem is I cant return any value.

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

I know that variable inside a try-catch block can't be accessed outside it but still I want to.

我知道 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){}
}   

it's a super serious problem for me in my project. I googled, and tried myself, but nothing solved.

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

I hope that now I will find some solution. I know that it has error saying return statement missingbut I want program exactly working like this.

我希望现在我能找到一些解决办法。我知道它有错误说缺少 return 语句,但我希望程序完全像这样工作。

Now the reason I'm strict to this

现在我对这个很严格原因

In my jar file I have to access text files for finding values 1 or 0 if "1" then activate else deactivate.

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

that is why I am using Boolean.

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

回答by Lucas

Just declare the boolean outside of the try/catch, and set the value in the try block

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

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

回答by rgettman

In your method, if an Exceptionis thrown, then there is no return statement. Place a returnstatement either in the exception handler, in a finallyblock, or after the exception handler.

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

回答by Colin D

The error is that you are not returning anything in the case where an exception is thrown.

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

try the following:

尝试以下操作:

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

回答by Abhishek

Declare the String strLine brfore the try/catch block
and then write your if statement after the try/catch block like

在 try/catch 块之前声明 String strLine,然后在 try/catch 块
之后编写 if 语句,例如

    String strLine;

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

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

    return true;    

get rid of the else block.

摆脱 else 块。

回答by Prince Sharma

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

Make sure you write import java.io.*before you write your program. Now You will be able to return the function even outside the tryand catch

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