Ruman Khan Pathan
Ruman Khan Pathan

Reputation: 137

How to access a variable outside a try catch block

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 a variable inside a try-catch block can't be accessed outside it, but still I want to access it somehow.

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){}
}   

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.

Upvotes: 10

Views: 39991

Answers (5)

Prince Sharma
Prince Sharma

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

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

Upvotes: 0

Colin D
Colin D

Reputation: 5661

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

Upvotes: 4

Abhishek
Abhishek

Reputation: 448

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

    String strLine;

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

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

    return true;    

get rid of the else block.

Upvotes: 0

rgettman
rgettman

Reputation: 178263

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.

Upvotes: 7

Lucas
Lucas

Reputation: 14959

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

Upvotes: 16

Related Questions