Nesting try blocks inside catch

I

iftekhar

hi there ,

consider the followinf code
#include <iostream>
#include <stdexcept>

using namespace std;

int main (void)
{
try
{
int i = 5;
throw i;
}
catch (int j)
{
try
{
double d = 4.5;
throw d;
}
catch(...)
{
cout << "got d" << endl;
try
{
int k = 5;
throw k;
}
catch (...)
{
float f = 5.5;
throw f;
}
}
}
catch(float g)
{
cout << "got float g" << endl;
}
catch (...)
{
cout << "unknown error" << endl;
}
cout << "done" << endl;
}
compiled with gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-52)


I expected the output of the program to be
got d
got float g
done

but the output is just
got d

i tried to debug it and it seems that the ' throw f; ' causes the
program to receive SIGABRT
and try to do an assembly level debug revealed it fails in the

__cxa_throw function and then calls the terminate

in a similar situation where objects are thrown rather than intrinsic
types it throws logic_error

any one have any idea why?

thanks for reading.
 
Z

Zara

hi there ,

consider the followinf code
#include <iostream>
#include <stdexcept>

using namespace std;

int main (void)
{
// *** TRY _BLOCK A, any exception will be catched by CATCH_BLOCK_A_#
try
{
int i = 5;
throw i;
}
// *** CATCH_BLOCK_A_1 will catch only from TRY_BLOCK_A
catch (int j)
{
// *** TRY _BLOCK B, any exception will be catched by CATCH_BLOCK_B
try
{
double d = 4.5;
throw d;
}
// *** CATCH_BLOCK_B will catch only from TRY_BLOCK_B
catch(...)
{
cout << "got d" << endl;
// *** TRY _BLOCK C, any exception will be catched by CATCH_BLOCK_C
try
{
int k = 5;
throw k;
}
// *** CATCH_BLOCK_C will catch only from TRY_BLOCK_C
catch (...)
{
float f = 5.5;
throw f;
}
}
}
// *** CATCH_BLOCK_A_2 will catch only from TRY_BLOCK_A
catch(float g)
{
cout << "got float g" << endl;
}
// *** CATCH_BLOCK_A_3 will catch only from TRY_BLOCK_A
catch (...)
{
cout << "unknown error" << endl;
}
cout << "done" << endl;
}
compiled with gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-52)


I expected the output of the program to be
got d
got float g
done

but the output is just
got d

As I hope that comments inserted will make clear, catches are assigned
to tries. Once yopu are inside a catch, you are *outside* of the
corresponding try. So your exception "i" exits TRY_BLOCK_A and enters
CATCH_BLOCK_A_1. Exception "d" is thrown, thus exiting TRY_BLOCK_B and
entering CATCH_BLOCK_B ("got d"). Exception "k" is thrown, exiting
TRY_BLOCK_ C and entering CATCH_BLOCK_C. Exception "f" is thrown, and
as there is no try block surrounding it, it is an unhandled exception.

Rememeber: To be catched, an exception must be *surrounded* by a try
block. When one is in a catch block, then it is outside of its
corresponding try block.

This is atndard, it si not compiler dependent.

Best regards,

Zara
 
I

iftekhar

Hi,
i got it this morning too. But my problem was not exactly this. The
real code was throwing logic_error because i was passing NULL ponter to
basic_string<>. And since i was debuging with eclipse i could not go
very deep.
thanks
iftekhar
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top