I'm trying to return a boolean value from my function having try-catch block,
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.
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 missing but I want program exactly working like this.
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.
that is why I am using Boolean.
Just declare the boolean outside of the try/catch, and set the value in the try block
public boolean myMethod() {
boolean success = false;
try {
doSomethingThatMightThrowAnException();
success = true;
}
catch ( Exception e ) {
e.printStackTrace();
}
return success;
}
In your method, if an Exception
is thrown, then there is no return statement. Place a return
statement either in the exception handler, in a finally
block, or after the exception handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With