 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
petke Guest
|
Posted: Tue Jul 25, 2006 5:25 am Post subject: howto catch nothing? |
|
|
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
|
Posted: Tue Jul 25, 2006 6:22 am Post subject: Re: howto catch nothing? |
|
|
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/ .
[ 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
|
Posted: Wed Jul 26, 2006 6:03 am Post subject: Re: howto catch nothing? |
|
|
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 |
|
 |
|
|
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
|
|