Newbie - Try, Catch & Return

J

Jacky

Hi,

I can't seemed to return a value and using the try/catch in
combination.

Main Program
-> Calls another program to open a file and returns the data as a
string

OpenFileMenu
-> Try
-> Open file
-> Append to string
-> Catch
-> Return

But it will mentioned it doesn't reconigse the return variable, it's
declared within the try and catch.

If I were to move the Return statement to within the try/catch, it
will prompt no return statement.

================================================================
--------------------Configuration: <Default>--------------------
C:\Documents and Settings\user\Desktop\Project\src\Project.java:86:
cannot resolve symbol
symbol : variable ret_value
location: class Project
return ret_value;
^
Note: C:\Documents and Settings\user\Desktop\Project\src\Project.java
uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
1 error
================================================================
public static void main(String[] args) throws Exception {
String file = OpenFileMenu();
}

static String OpenFileMenu() {
try {
//opening file
FileInputStream fstream = new FileInputStream(filename);

//converting input stream to DataInputStream
DataInputStream in = new DataInputStream(fstream);

StringBuffer bigBuf = new StringBuffer();
// reading file
while (in.available() != 0)
bigBuf = bigBuf.append(in.readLine());

String ret_value = bigBuf.toString();

//closing file
in.close();
}

catch (Exception e) {
System.err.println("File input error");
}

return ret_value;
}
================================================================
 
V

vbhelpski

I think this is a scoping problem. If you define something within
the try block, it will not be there when that block is exited.
 
R

Rivky

Scoping issue:

Your problem is that your ret_value string is declared within the try
brackets. Declare the String outside the try block. Like this:

String ret_value;

static String OpenFileMenu() {
try {
//opening file
FileInputStream fstream = new
FileInputStream(filename);


//converting input stream to DataInputStream
DataInputStream in = new
DataInputStream(fstream);


StringBuffer bigBuf = new StringBuffer();
// reading file
while (in.available() != 0)
bigBuf = bigBuf.append(in.readLine());


ret_value = bigBuf.toString();


//closing file
in.close();
}


catch (Exception e) {
System.err.println("File input error");
}


return ret_value;
 
B

Bryce

The error message tells it all. Remember, try... starts a new block,
and either catch or finally ends the block.

With that said:

public static void main(String[] args) throws Exception {
String file = OpenFileMenu();
}

static String OpenFileMenu() {
try {
//opening file
FileInputStream fstream = new FileInputStream(filename);

//converting input stream to DataInputStream
DataInputStream in = new DataInputStream(fstream);

StringBuffer bigBuf = new StringBuffer();
// reading file
while (in.available() != 0)
bigBuf = bigBuf.append(in.readLine());

String ret_value = bigBuf.toString();

+++++++++++++++++++++++++++++++++++++++++++++++++++
++++++ ret_value is defined inside the try bock
+++++++++++++++++++++++++++++++++++++++++++++++++++
//closing file
in.close();
}

catch (Exception e) {
System.err.println("File input error");
}

return ret_value;
}

And you are returning it outside the try block. Rewrite it like this:

static String OpenFileMenu() {
String ret_value = null;
try {
...
} catch (Exception e) {
...
}
return ret_value;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top