Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #18734
| 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
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.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
can't return value "bilsch" <king621@comcast.net> - 2012-09-12 00:36 -0700
Re: can't return value Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-12 07:11 -0400
Re: can't return value bilsch <bilsch01@gmail.com> - 2012-09-12 22:15 -0700
Re: can't return value Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-13 08:53 -0400
Re: can't return value Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2012-09-12 15:32 +0300
Re: can't return value bilsch <bilsch01@gmail.com> - 2012-09-12 22:37 -0700
Re: can't return value Lew <lewbloch@gmail.com> - 2012-09-12 23:17 -0700
Re: can't return value markspace <-@.> - 2012-09-13 01:11 -0700
Re: can't return value Lew <lewbloch@gmail.com> - 2012-09-13 10:53 -0700
Re: can't return value Stuart <DerTopper@web.de> - 2012-09-13 09:45 +0200
Re: can't return value "bilsch" <king621@comcast.net> - 2012-09-13 03:55 -0700
Re: can't return value Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2012-09-13 15:06 +0300
Re: can't return value Stuart <DerTopper@web.de> - 2012-09-13 20:53 +0200
Re: can't return value Roedy Green <see_website@mindprod.com.invalid> - 2012-09-12 20:54 -0700
csiph-web