Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer,comp.lang.java.help Subject: Re: can't return value Followup-To: comp.lang.java.help Date: Thu, 13 Sep 2012 08:53:23 -0400 Organization: A noiseless patient Spider Lines: 53 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 13 Sep 2012 12:53:26 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="ffb8f7085759b339c1002252b48331a4"; logging-data="24719"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+yD4/DUtfB5nHUXhUgN1Qa" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120907 Thunderbird/15.0.1 In-Reply-To: Cancel-Lock: sha1:4zcZeQly+mkqRisD+7ws7N9pgxY= Xref: csiph.com comp.lang.java.programmer:18734 comp.lang.java.help:2092 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."