exception throw and handle and resume

R

Rahul

Hi Everyone,

I have the following code,

int main()
{
bool continue1 = true;
while(continue1)
{
try
{
printf("executed\n");
throw 5;
continue1 = false;
}
catch(int)
{
printf("exception caught, and about to retry also\n");
continue1 = true;
}
}
return(0);
}

So it turns out to be a recursive throw and handling of the integer
exception. the int 5 is allocated in the stack and therefore i assume
that it is passed to the catch handler by value as a function call...
if that is the case, is it similar to recursive function call and
would cause a stack overflow?

Thanks in advance!!!
 
J

Jim Langston

Rahul said:
Hi Everyone,

I have the following code,

int main()
{
bool continue1 = true;
while(continue1)
{
try
{
printf("executed\n");
throw 5;
continue1 = false;
}
catch(int)
{
printf("exception caught, and about to retry also\n");
continue1 = true;
}
}
return(0);
}

So it turns out to be a recursive throw and handling of the integer
exception. the int 5 is allocated in the stack and therefore i assume
that it is passed to the catch handler by value as a function call...
if that is the case, is it similar to recursive function call and
would cause a stack overflow?

Thanks in advance!!!

The entire try...catch block is completed before the next execution, so the
thrown variable is cleaned up so you won't have that issue.
 
R

Rahul

Hi Everyone,

I have the following code,

int main()
{
bool continue1 = true;
while(continue1)
{
try
{
printf("executed\n");
throw 5;
continue1 = false;
}
catch(int)
{
printf("exception caught, and about to retry also\n");
continue1 = true;
}
}
return(0);

}

So it turns out to be a recursive throw and handling of the integer
exception. the int 5 is allocated in the stack and therefore i assume
that it is passed to the catch handler by value as a function call...
if that is the case, is it similar to recursive function call and
would cause a stack overflow?

Thanks in advance!!!

Does the standard say anything about memory holding the integer 5?
As per the block scope rules, 5 (local variable) would be deallocated
in that block,
if that is the case, how is the value passed to the exception block?
 
P

Pete Becker

Does the standard say anything about memory holding the integer 5?
As per the block scope rules, 5 (local variable) would be deallocated
in that block,
if that is the case, how is the value passed to the exception block?

There's a bunch of mechansim behind the curtains. Essentially, the
compiler copies the value to someplace safe.
 
T

terminator

There's a bunch of mechansim behind the curtains. Essentially, the
compiler copies the value to someplace safe.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

the final object resulting from throw is in catch context and is
destructed by leaving the catch block(after all it is a block).

on a considerable compiler you must not get into trouble with stack
for the above snippet.

regards,
FM.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top