473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Nesting try blocks inside catch

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.

Jan 24 '06 #1
2 6145
On 24 Jan 2006 11:46:11 -0800, "iftekhar" <ah************@gmail.com>
wrote:
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 <snip>


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
Jan 25 '06 #2
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

Jan 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
8
by: Adam H. Peterson | last post by:
Hello, I sometimes find myself writing code something like this: try { Derived &d=dynamic_cast<Derived&>(b); d.do_something_complicated(); // etc.... } catch (std::bad_cast) { throw...
4
by: Chris Martin | last post by:
Below are three try-catch blocks. My question is, how can all three work, and how come there is no memory leak (or is there)? As I understand it, an exception object is being created and "thrown"...
5
by: Mike P | last post by:
I'm trying to split up a large procedure into several try catch blocks in order to catch different errors. However, when I split my code up like this, my variables that I am using throughout the...
5
by: Steve Murphy | last post by:
I have a couple of standard Try/Catch blocks that I use repeatedly. Is there anyway to wrap these blocks into a method call? Or does C# have anything like a C++ macro? Thanks, Steve Murphy
26
by: Grim Reaper | last post by:
Just a quick and probably daft question... Isn't the code; Try DoSomething() Catch e As Exception HandleError(e) Finally DoThisAtTheEnd()
3
by: stktung | last post by:
Hi, I'm not too familiar with how exception works in .NET but I've read from somewhere that in C++, code that are ran in try blocks are expensive: ...
4
by: lovecreatesbea... | last post by:
Is the following code (without any code at lines x and x + 3) correct? Is it better to call exit() or re-throw the exceptions at line x and line x + 3?. What is the better code should we place at...
9
by: GiJeet | last post by:
Hello, I come from the VB6 world where we'd put a single ON ERROR GOTO ErrHandler at the top of a method. Now whenever an error happened it would drop into the ErrHandler code. In .Net it seems...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.