C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

howto catch nothing?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
petke
Guest





PostPosted: Tue Jul 25, 2006 5:25 am    Post subject: howto catch nothing? Reply with quote



Hi

I was wondering what happens when you throw nothing. As in how does one
catch that. If you cant I wonder why compilers allow throws like this.

try {
throw;
} catch( ... ) { // what goes here?
;
}

/Petke


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
red floyd
Guest





PostPosted: Tue Jul 25, 2006 6:22 am    Post subject: Re: howto catch nothing? Reply with quote



petke wrote:
Quote:
Hi

I was wondering what happens when you throw nothing. As in how does one
catch that. If you cant I wonder why compilers allow throws like this.

try {
throw;
} catch( ... ) { // what goes here?
;
}

/Petke


It rethrows the current exception (15.1/6-7). If no exception is
currently being handled, terminate() is called (15.1/Cool.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Bob Bell
Guest





PostPosted: Wed Jul 26, 2006 6:03 am    Post subject: Re: howto catch nothing? Reply with quote



petke wrote:
Quote:
I tried to catch a exeption by reference and
modify it, but it seems the exception gets thrown by value anyway. Am I
missing something, or maybe exceptions and references just dont mix?

int i = 0;
try {
int &ex = i;
ex++;
throw ex;
} catch(int &ex) { //i=1, ex=1
try {
ex++;
throw; //re-throw
} catch(int &ex) { //i=1, ex=2
ex++;//"catched re-trow"
} //i=1, ex=3
}

When you "throw ex;" it makes a copy of the value you are throwing (if
I remember correctly, using the copy constructor). Thereafter, in the
catch blocks, "ex" is a reference to the copy, so you can modify it if
you wish.

The reason the copy is made is that the original object may not exist,
as in:

try {
int i;
int& ex(i);
throw ex;
}
catch (int& ex) {
// i no longer exists; ex must not refer to it.
}

Bob


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.