Connecting Tech Pros Worldwide Forums | Help | Site Map

C++: rethrow in exception handling

A
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi,

I'm having some difficulty understanding the semantics of the rethrow
keyword. Consider the following code:

int main(){
try{
...
}
catch(SomeException &SE){
...
}
catch(...){
rethrow;
}
return 0;
}


What is the point of rethrowing the exception caught at catch(...)? Won't
this result in a endless cycle effect?


Regards,
A



Domenico Andreoli
Guest
 
Posts: n/a
#2: Jul 22 '05

re: C++: rethrow in exception handling


A wrote:[color=blue]
> Hi,
>[/color]
hi
[color=blue]
> What is the point of rethrowing the exception caught at catch(...)? Won't
> this result in a endless cycle effect?
>[/color]
no.

you can use it to do some cleaning before letting the exception reach
the next handler, as if you didn't caught it. in your example you'll let
the exception be trapped by the default handler which usually aborts you
app.


-----[ Domenico Andreoli, aka cavok
--[ http://filibusta.crema.unimi.it/~cavok/gpgkey.asc
---[ 3A0F 2F80 F79C 678A 8936 4FEE 0677 9033 A20E BC50

tom_usenet
Guest
 
Posts: n/a
#3: Jul 22 '05

re: C++: rethrow in exception handling


On Tue, 18 Nov 2003 21:35:03 +1030, "A" <A@iprimus.com.au> wrote:
[color=blue]
>Hi,
>
>I'm having some difficulty understanding the semantics of the rethrow
>keyword.[/color]

There is no rethrow keyword. I'm going to assume you mean throw.


Consider the following code:[color=blue]
>
>int main(){
> try{
> ...
> }
> catch(SomeException &SE){
> ...
> }
> catch(...){
> rethrow;[/color]

Rather:
throw;
[color=blue]
> }
>return 0;
>}
>
>
>What is the point of rethrowing the exception caught at catch(...)? Won't
>this result in a endless cycle effect?[/color]

No, the throw above will just cause the program to terminate (since
the exception will propogate out of main. throw; is used when you want
to do a bit of clean up in the current scope, and then let the
exception propogate up. However, it should be used sparingly - RAII is
a much better technique to handle exception cleanup.

Tom
A
Guest
 
Posts: n/a
#4: Jul 22 '05

re: C++: rethrow in exception handling



"tom_usenet" <tom_usenet@hotmail.com> wrote in message
news:r43krvccm3qhjprh2kdahnc2tf3j0j3pb3@4ax.com...[color=blue]
> On Tue, 18 Nov 2003 21:35:03 +1030, "A" <A@iprimus.com.au> wrote:
>[color=green]
> >Hi,
> >
> >I'm having some difficulty understanding the semantics of the rethrow
> >keyword.[/color]
>
> There is no rethrow keyword. I'm going to assume you mean throw.
>
>
> Consider the following code:[color=green]
> >
> >int main(){
> > try{
> > ...
> > }
> > catch(SomeException &SE){
> > ...
> > }
> > catch(...){
> > rethrow;[/color]
>
> Rather:
> throw;
>[color=green]
> > }
> >return 0;
> >}
> >
> >
> >What is the point of rethrowing the exception caught at catch(...)? Won't
> >this result in a endless cycle effect?[/color]
>
> No, the throw above will just cause the program to terminate (since
> the exception will propogate out of main. throw; is used when you want
> to do a bit of clean up in the current scope, and then let the
> exception propogate up. However, it should be used sparingly - RAII is
> a much better technique to handle exception cleanup.
>
> Tom[/color]

What do you mean by propogate up? what goes up must come down again? and so
it repeats itself.


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.538 / Virus Database: 333 - Release Date: 10/11/2003


Severin Ecker
Guest
 
Posts: n/a
#5: Jul 22 '05

re: C++: rethrow in exception handling


hi!
[color=blue][color=green][color=darkred]
> > > catch(...){
> > > throw;[/color][/color][/color]
changed this to throw...[color=blue][color=green][color=darkred]
> > > }
> > >return 0;
> > >}[/color][/color]
> What do you mean by propogate up? what goes up must come down again? and[/color]
so[color=blue]
> it repeats itself.[/color]
here you throw the expection within the catch. now looking for an
appropriate exception handler starts all over again.
it's not been called within a try in main, so the thrown exception
propagates out of main, and there the default exception handler "is
waiting", which normally terminates the app.

note: throwing an exception in a catch() block does not result in getting
caught within this catch() block, it rather "needs to be thrown" within a
try block for the exception handling to work (except for the default handler
outside of main)

regards,
sev


tom_usenet
Guest
 
Posts: n/a
#6: Jul 22 '05

re: C++: rethrow in exception handling


On Wed, 19 Nov 2003 19:27:41 +1030, "A" <A@iprimus.com.au> wrote:
[color=blue]
>What do you mean by propogate up? what goes up must come down again? and so
>it repeats itself.[/color]

I mean propogate up the stack to the function that called the one the
throw; is in. I think you misunderstand what throw; actually does. It
doesn't rethrow the exception from where it was originally thrown, it
throws it from the point of the throw;. Here's an example:

#include <iostream>

class test_exception{};

void foo(int size)
{
std::cout << '3';
int* i = new int[size];
std::cout << '4';
try
{
std::cout << '5';
throw test_exception(); //*
std::cout << "Never here";
}
catch(...)
{
std::cout << '6';
delete[] i;
std::cout << '7';
throw; //throws the exception from here, not from *
std::cout << "Never here";
}
std::cout << "Never here";
}

int main()
{
std::cout << '1';
try
{
std::cout << '2';
foo(100);
std::cout << "Never here";
}
catch(test_exception const&)
{
std::cout << '8';
}
std::cout << '\n';
}

Tom
Closed Thread