"missing return statement" error [Solved]
-
I have a function that is similar to the following:
public int LoadFromFile(String fileName) {
FileReader reader = null;
try {
int rval = 0;
reader = new FileReader(fileName);
// Do some stuff to get 'rval' from the file
return rval;
} finally {
if (reader != null) reader.close();
}
}I don't understand why I am getting the compile-time error, "missing return statement". Either execution gets to the line "return rval;", or an exception is thrown and no value needs to be returned. I can get the compile-time error to go away if I move the return value declaration prior to the try block and move the return statement after the finally block (I read online that putting a return statement within the finally block suppresses all exceptions), but I don't understand why I need to do that. Thanks for any help,
Sounds like somebody's got a case of the Mondays -Jeff
-
I have a function that is similar to the following:
public int LoadFromFile(String fileName) {
FileReader reader = null;
try {
int rval = 0;
reader = new FileReader(fileName);
// Do some stuff to get 'rval' from the file
return rval;
} finally {
if (reader != null) reader.close();
}
}I don't understand why I am getting the compile-time error, "missing return statement". Either execution gets to the line "return rval;", or an exception is thrown and no value needs to be returned. I can get the compile-time error to go away if I move the return value declaration prior to the try block and move the return statement after the finally block (I read online that putting a return statement within the finally block suppresses all exceptions), but I don't understand why I need to do that. Thanks for any help,
Sounds like somebody's got a case of the Mondays -Jeff
-
The above code does work. Turns out I had the return statement within a while loop on accident, so if the condition was initially false I wouldn't have thrown an error nor returned a value.
Sounds like somebody's got a case of the Mondays -Jeff
:thumbsup:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.