Article View: aus.computers.java
Article #197Re: throwing exceptions
From: Ingo Pakleppa
Date: Sun, 20 Apr 2003 11:11
Date: Sun, 20 Apr 2003 11:11
46 lines
2956 bytes
2956 bytes
On Sun, 20 Apr 2003 11:14:58 +0200, Mike Crenshaw wrote:
>> As well as closing the pod bay doors, you may also need to notify the
> object
>> that called the method that an exception has been thrown, otherwise it may
>> not realise that something went wrong.
>
> hmm
> ok so where would it be recaught?
In whoever called the method. A more complete example is below. This also
illustrates one example why you would actually want to do this. enterPod
is a low-level method that actually enters the pod, and cleans up if
something went wrong. It doesn't know what kind of user interface you
have, and therefore cannot report any errors. But it does know that the
pod door needs to be closed if something went wrong.
The higher level method getIntoPod() knows how to actually tell the user
that something went wrong, but shouldn't have to deal with the details of
opening or closing the doors.
(Side note: in real life applications, the exception would often not be
rethrown verbatim because it, too, is low-level information. Rather, it
would be wrapped into another exception. But that's immaterial here).
void enterPod() throws CannotEnterPodException
{
hal.openPodBayDoors();
try {
pod.enter();
}
catch (CannotEnterPodException e) {
hal.closePodBayDoors();
throw e;
}
}
void getIntoPod()
{
try {
enterPod();
}
catch (CannotEnterPodException e) {
System.out.println( "Sorry, I cannot do that, Dave" );
}
}
Technical Details
Message-ID:
<pan.2003.04.20.11.11.06.499041@kkeane.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.cs.ubc.ca!nntp-relay.ihug.net!ihug.co.nz!west.cox.net!cox.net!p01!fed1read04.POSTED!not-for-mailReferences:
<puIna.11699$hF.92186@news-server.bigpond.net.au> <qDIna.110$KS4.52@news.get2net.dk> <ryTna.20910$y3.1898552@news010.worldonline.dk> <3EA0185D.C7B1A753@sun.com> <Xxboa.22515$y3.1976088@news010.worldonline.dk> <Ftkoa.2137$8K2.15274@news-server.bigpond.net.au> <0ytoa.24351$y3.2031961@news010.worldonline.dk>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f5d71,36528a6761383b70 X-Google-Attributes: gidf5d71,public X-Google-Thread: 10d6db,36528a6761383b70 X-Google-Attributes: gid10d6db,public X-Google-Thread: 113ae9,36528a6761383b70 X-Google-Attributes: gid113ae9,public X-Google-ArrivalTime: 2003-04-20 04:11:29 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.cs.ubc.ca!nntp-relay.ihug.net!ihug.co.nz!west.cox.net!cox.net!p01!fed1read04.POSTED!not-for-mail From: "Ingo Pakleppa" <ingo-immigration@kkeane.com> Subject: Re: throwing exceptions User-Agent: Pan/0.13.3 (That cat's something I can't explain) Message-ID: <pan.2003.04.20.11.11.06.499041@kkeane.com> Newsgroups: aus.computers.java,comp.lang.java,comp.lang.java.help References: <puIna.11699$hF.92186@news-server.bigpond.net.au> <qDIna.110$KS4.52@news.get2net.dk> <ryTna.20910$y3.1898552@news010.worldonline.dk> <3EA0185D.C7B1A753@sun.com> <Xxboa.22515$y3.1976088@news010.worldonline.dk> <Ftkoa.2137$8K2.15274@news-server.bigpond.net.au> <0ytoa.24351$y3.2031961@news010.worldonline.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Lines: 47 Date: Sun, 20 Apr 2003 04:11:08 -0700 NNTP-Posting-Host: 68.7.157.187 X-Complaints-To: abuse@cox.net X-Trace: fed1read04 1050837086 68.7.157.187 (Sun, 20 Apr 2003 07:11:26 EDT) NNTP-Posting-Date: Sun, 20 Apr 2003 07:11:26 EDT Organization: Cox Communications