Thread View: aus.computers.java
15 messages
15 total messages
Started by edwin taitz
Fri, 18 Apr 2003 01:25
throwing exceptions
Author: edwin taitz
Date: Fri, 18 Apr 2003 01:25
Date: Fri, 18 Apr 2003 01:25
2 lines
1334 bytes
1334 bytes
is it possible to rethrow an exception. and if so how??
Re: throwing exceptions
Author: S�ren Bak
Date: Fri, 18 Apr 2003 02:33
Date: Fri, 18 Apr 2003 02:33
21 lines
1672 bytes
1672 bytes
You can do it like this:
try {
// throw an exception here
...
} catch (Throwable e) {
// process the exception here
...
// and re-throw it
throw e;
}
Regards,
S�ren Bak
"edwin taitz" <taitz@bigpond.net.au> skrev i en meddelelse
news:puIna.11699$hF.92186@news-server.bigpond.net.au...
> is it possible to rethrow an exception.
> and if so how??
>
>
Re: throwing exceptions
Author: Darryl L. Pierce
Date: Fri, 18 Apr 2003 13:35
Date: Fri, 18 Apr 2003 13:35
25 lines
1637 bytes
1637 bytes
In comp.lang.java.help edwin taitz <taitz@bigpond.net.au> wrote:
> is it possible to rethrow an exception.
> and if so how??
try
{
istream = new FileInputStream("foo.txt");
}
catch(IOException error)
{
System.out.println("Exception thrown: " + error);
error.printStackTrace();
// here's what's important to you
throw error;
}
--
/**
* @author Darryl L. Pierce <mcpierce@myrealbox.com>
* @see The J2ME FAQ <http://bellsouthpwp.net/m/c/mcpierce/j2mefaq.html">
* @quote "What do you care what others think, Mr. Feynman?"
* @geek echo '$_ = "Jvtu bopuifs Pfsm ibdlfs."; y/a-z/za-y/; print' | perl
*/
Re: throwing exceptions
Author: Mike Crenshaw
Date: Fri, 18 Apr 2003 14:01
Date: Fri, 18 Apr 2003 14:01
3 lines
1397 bytes
1397 bytes
> throw e; whats the point of rethrowing exceptions?
Re: throwing exceptions
Author: Eric Sosman
Date: Fri, 18 Apr 2003 15:23
Date: Fri, 18 Apr 2003 15:23
20 lines
1869 bytes
1869 bytes
Mike Crenshaw wrote:
>
> > throw e;
>
> whats the point of rethrowing exceptions?
Cleaning up before giving up:
hal.openPodBayDoors();
try {
pod.enter();
}
catch (CannotEnterPodException e) {
hal.closePodBayDoors();
throw e;
}
...
--
Eric.Sosman@sun.com
Re: throwing exceptions
Author: Mike Crenshaw
Date: Sat, 19 Apr 2003 12:45
Date: Sat, 19 Apr 2003 12:45
9 lines
1723 bytes
1723 bytes
> Cleaning up before giving up:
> catch (CannotEnterPodException e) {
> hal.closePodBayDoors();
> throw e;
> }
hmm i still dont follow you.. you can execute the hal.closePodBayDoors()
without the throw e anyway cant you?
Re: throwing exceptions
Author: S�ren Bak
Date: Sat, 19 Apr 2003 19:08
Date: Sat, 19 Apr 2003 19:08
19 lines
2063 bytes
2063 bytes
"Mike Crenshaw" <crenshaw@worldonline.dk> skrev i en meddelelse
news:Xxboa.22515$y3.1976088@news010.worldonline.dk...
> > Cleaning up before giving up:
>
> > catch (CannotEnterPodException e) {
> > hal.closePodBayDoors();
> > throw e;
> > }
>
> hmm i still dont follow you.. you can execute the hal.closePodBayDoors()
> without the throw e anyway cant you?
>
>
You know that you should invoke hal.closePodBayDoors exactly because you
catch the exception. If you don't catch it, there is no-one who knows how to
clean up. But that is not the same as saying that the exception should not
be passed further on.
Re: throwing exceptions
Author: Moth
Date: Sat, 19 Apr 2003 22:55
Date: Sat, 19 Apr 2003 22:55
18 lines
2050 bytes
2050 bytes
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.
"Mike Crenshaw" <crenshaw@worldonline.dk> wrote in message
news:Xxboa.22515$y3.1976088@news010.worldonline.dk...
> > Cleaning up before giving up:
>
> > catch (CannotEnterPodException e) {
> > hal.closePodBayDoors();
> > throw e;
> > }
>
> hmm i still dont follow you.. you can execute the hal.closePodBayDoors()
> without the throw e anyway cant you?
>
>
Re: throwing exceptions
Author: Mike Crenshaw
Date: Sun, 20 Apr 2003 09:14
Date: Sun, 20 Apr 2003 09:14
7 lines
1777 bytes
1777 bytes
> 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?
Re: throwing exceptions
Author: 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" );
}
}
Re: throwing exceptions
Author: Darryl L. Pierce
Date: Sun, 20 Apr 2003 12:28
Date: Sun, 20 Apr 2003 12:28
14 lines
1960 bytes
1960 bytes
In comp.lang.java.help Mike Crenshaw <crenshaw@worldonline.dk> wrote: > ok so where would it be recaught? Any method that (re)throws an exception has to declare such explicitly. It would be recaught wherever it's logical and appropriate in your application, higher up the call stack. -- /** * @author Darryl L. Pierce <mcpierce@myrealbox.com> * @see The J2ME FAQ <http://bellsouthpwp.net/m/c/mcpierce/j2mefaq.html"> * @quote "What do you care what others think, Mr. Feynman?" * @geek echo '$_ = "Jvtu bopuifs Pfsm ibdlfs."; y/a-z/za-y/; print' | perl */
Re: throwing exceptions
Author: Mike Crenshaw
Date: Sun, 20 Apr 2003 13:42
Date: Sun, 20 Apr 2003 13:42
1 lines
1621 bytes
1621 bytes
Well doing an experiment i think i actually get it now:)
Re: throwing exceptions
Author: .Saphyr
Date: Mon, 21 Apr 2003 19:05
Date: Mon, 21 Apr 2003 19:05
13 lines
1630 bytes
1630 bytes
> hal.openPodBayDoors();
> try {
> pod.enter();
> }
> catch (CannotEnterPodException e) {
> hal.closePodBayDoors();
> throw e;
> }
My question might sound basic but whats the point
with the 'finally' statement ? Shouldn't it be used
specifically for those cases ?
Re: throwing exceptions
Author: Daniel Dyer
Date: Mon, 21 Apr 2003 19:45
Date: Mon, 21 Apr 2003 19:45
23 lines
1992 bytes
1992 bytes
On Mon, 21 Apr 2003 21:05:59 +0200, .Saphyr <saphyr@infomaniak.ch> wrote:
>> hal.openPodBayDoors();
>> try {
>> pod.enter();
>> }
>> catch (CannotEnterPodException e) {
>> hal.closePodBayDoors();
>> throw e;
>> }
>
>
> My question might sound basic but whats the point
> with the 'finally' statement ? Shouldn't it be used
> specifically for those cases ?
>
Yes, assuming you want the doors closed regardless of whether pod.enter()
succeeds, try...finally would be a better solution. However if your logic
is such that you require the doors to be closed only in an exceptional
circumstance, you would need to catch the exception and re-throw it.
Dan.
Re: throwing exceptions
Author: Darryl L. Pierce
Date: Tue, 22 Apr 2003 11:09
Date: Tue, 22 Apr 2003 11:09
26 lines
1949 bytes
1949 bytes
In comp.lang.java.help .Saphyr <saphyr@infomaniak.ch> wrote:
>> hal.openPodBayDoors();
>> try {
>> pod.enter();
>> }
>> catch (CannotEnterPodException e) {
>> hal.closePodBayDoors();
>> throw e;
>> }
>
>
> My question might sound basic but whats the point
> with the 'finally' statement ? Shouldn't it be used
> specifically for those cases ?
No. You may only want to close the pod bay doors if there's a problem
while trying to open them. Finally will _always_ execute; catch() will
only execute on an exception...
--
/**
* @author Darryl L. Pierce <mcpierce@myrealbox.com>
* @see The J2ME FAQ <http://bellsouthpwp.net/m/c/mcpierce/j2mefaq.html">
* @quote "What do you care what others think, Mr. Feynman?"
* @geek echo '$_ = "Jvtu bopuifs Pfsm ibdlfs."; y/a-z/za-y/; print' | perl
*/
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads