Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2092

Re: can't return value

From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.java.programmer, comp.lang.java.help
Subject Re: can't return value
Followup-To comp.lang.java.help
Date 2012-09-13 08:53 -0400
Organization A noiseless patient Spider
Message-ID <k2sl06$o4f$1@dont-email.me> (permalink)
References <k2pe2b$lp4$1@dont-email.me> <k2pql5$o74$1@dont-email.me> <k2rq4k$c2g$1@dont-email.me>

Cross-posted to 2 groups.

Followups directed to: comp.lang.java.help

Show all headers | View raw


On 9/13/2012 1:15 AM, bilsch wrote:
> On 9/12/2012 4:11 AM, Eric Sosman wrote:
>> On 9/12/2012 3:36 AM, bilsch wrote:
>>>[...]
>> You need to decide what to do if there is no space in
>> `wholeName': throw an exception, return `null', whatever you
>> like -- but the compiler will not allow you to just ignore
>> the possibility.
>> [...]
> thanks for your reply.  I got it to work two different ways. Could you
> explain returning NULL? Also, I don't know what exception to throw or
> how to do it.

     Any variable that refers to an object -- a String, a List,
or whatever -- can have the special value `null' (not `NULL')
to indicate that it "refers to nothing" at the moment.  Methods
that return object references can return `null' to indicate
"I've got nothing to give you."  If your method returns, it
must return some value; I'm suggesting that if your method
could not do its job, `null' is a value you might consider
returning.  It's just a special value you might decide should
mean "I couldn't find a last name in `wholeName'."

     Another possibility is to throw an exception: The method
tries to find a last name, discovers that `wholeName' doesn't
contain one, and says "Hey, stupid caller: You fed me garbage!"
In the case at hand, IllegalArgumentException seems a likely
candidate, so the method could announce its displeasure with

	throw new IllegalArgumentException(
	    "no last name in " + wholeName);

When a method terminates by throwing an exception it does not
need to return a value, because in truth it doesn't "return"
at all: It abruptly stops what it was doing, and what all its
callers were doing, up to the point where some caller has a
`try {...} catch' for the type of exception thrown.

     Bilsch, this is very elementary stuff, the sort of thing
you will find in any introductory textbook or tutorial on Java.
I suggest you consult one; trying to learn the language one
corrected blunder at a time is not very efficient.  You might
also think about using comp.lang.java.help for elementary
questions; comp.lang.java.programmer is (in theory) a forum
for people who already know the basics and are tackling more
advanced issues.  (Note the "in theory.")

     CC'ed, and follow-ups set.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid
"The speed at which the system fails is usually not important."

Back to comp.lang.java.help | Previous | Next | Find similar | Unroll thread


Thread

Re: can't return value Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-13 08:53 -0400

csiph-web