Results 1 to 5 of 5
- 02-05-2012, 06:31 PM #1
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Missing return statement... but there is one
Hi,
Trivial problem, I know. But somehow I cannot figure it out.
I get missing return statement error. But wait... don't I return resultArray?Java Code:private static String[] checkAccountStatus() { try { String XMLUrl = "url"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new URL(XMLUrl).openStream()); XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xpath.compile("//result/*/text()"); Object result = expr.evaluate(document, XPathConstants.NODESET); NodeList nodes = (NodeList) result; String[] resultArray; resultArray = new String[nodes.getLength()]; for (int i = 0; i < nodes.getLength(); i++) { resultArray[i] = nodes.item(i).getNodeValue(); } return resultArray; } catch (Exception e) { e.printStackTrace(); } }
- 02-05-2012, 06:33 PM #2
Re: Missing return statement... but there is one
What happens if an Exception is thrown in the try block and execution passes to the catch(...) block? Where's the return statement then?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-05-2012, 06:40 PM #3
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Missing return statement... but there is one
I can't put return resultArray in the catch block. So what I'm supposed to do?
- 02-05-2012, 06:49 PM #4
Re: Missing return statement... but there is one
You can't and you shouldn't want to -- because if you hit the catch(...) block it means something has gone wrong and resultArray, if it's even initialized, doesn't contain valid data.
You have two options here: throw the Exception instead of catching it, and let the caller deal with it. This approach seems more correct to me than the alternative, which is to return null or a contrived String[] from after the catch block. In the second case, the caller will have to test the validity of the return value.
Have you gone through the Exceptions tutorial? Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-05-2012, 07:24 PM #5
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Similar Threads
-
Missing return statement
By Aenohe in forum New To JavaReplies: 8Last Post: 03-07-2012, 01:03 PM -
Missing Return statement =[
By avirunes in forum New To JavaReplies: 6Last Post: 02-12-2011, 10:34 AM -
Missing return statement
By gkoef in forum New To JavaReplies: 8Last Post: 01-01-2011, 02:52 AM -
missing return statement
By bayan in forum New To JavaReplies: 6Last Post: 04-26-2010, 03:15 PM -
Missing Return Statement Error
By darkblue24 in forum New To JavaReplies: 13Last Post: 02-16-2010, 08:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks