jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes How to return a value with a method that throws an exception... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to return a value with a method that throws an exception..." Watch "How to return a value with a method that throws an exception..." New topic
Author

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

Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 43
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 43
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
 
Don't get me started about those stupid light bulbs.
 
subject: How to return a value with a method that throws an exception...
 
Similar Threads
Content is not allowed in prolog (EDIT: emergency code fix help!)
BufferedReader .readLine() is not seeing end of file
Declaring exceptions in interfaces...
Counting newline characters in BufferedReader...
Java trying to read Telnet banner (help appreciated)