Wasn't sure if this was simple enough for the beginner forum, so I'm going to try it out here. :]
I'm still in the process of deciding how much I will use exceptions in my code. I do, however, currently have a situation in which I need to deal with an exception.
I've got a interface that represents a source of XML data. This could be any valid source of xml. A string, a text file, output from another program, whatever.
One implementation of this interface wraps a text file, and is called TextFileXMLSource. In this class I implement a method of the interface named readCharacter() that is supposed to return a Character. I am implementing this method by using a BufferedReader that wraps a text file.
The problem I'm having is this. I want to throw an exception if for some reason there is an I/O problem and I can't get the Character from the underlying BufferedReader. (I don't read directly from the BufferedReader in my implementation of the ReadCharacter, but use another private method in my class to do this. The private method takes care of casting the returned char to a Character and other housekeeping tasks.) In the body of the readCharacter method I attempt to obtain the Character in a try/catch block. If I can't get the Character I throw an exception. If I can get the character, I return it. However, the compiler is telling me that my method is not returning a Character object as it declares. I can't figure this out, as my method definitely has a "return" statement.
Here is my code:
Any ideas what might be wrong? Is my syntax somehow hiding the return statement from the compiler?
Are you sure the message is regarding this function? It's clear that all paths either return a value or throw an exception, and so it should be fine. Double-check that you're compiling this exact source and that the error message is referring to this exact method.
I owe you an apology. After reading your response I was able to recompile the above source code and remove the error message. I am not sure what went wrong the previous times I tried, but I must've changed something because I can now build the file with no error message.
Perhaps it is something I did in Eclipse, which is the IDE I am using.
I should have been more persistent with my efforts in the IDE before posting. Thank you for taking the time to respond. I was really banging my head against the wall on this one.
author and iconoclast