Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to return a value with a method that throws an exception...

 
Landon Blake
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks,

Landon
[ August 02, 2006: Message edited by: Landon Blake ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Landon Blake
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem at all. This kind of thing happens more often than any of us would like to admit!

Eclipse will show error messages that don't correspond to the current source code, and won't update them until you save the file; that may have tricked you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic