Nested exception throwing

U

usenet

I wrote some sample code (see below) for nested exception throwing
i.e. my catch blocks are throwing exceptions of their own (for
simplicity I used standard exceptions). I am getting some extraneous
characters (St9) in my final output. Can anyone kindly help me
interpret that please?

Thanks,
Ramesh



Here's my output:
Exception bad_alloc in function func_level3()
Exception: Exception: Exception: St9bad_alloc in function
func_level2() in function func_level1() at xcptn-test.cpp,74



Here's my sample code:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <sstream>
#include <stdexcept>
// Standard exceptions:-
// bad_alloc, bad_cast, bad_typeid,
bad_exception,
// logic_error, runtime_error,
ios_base::failure
// domain_error, invalid_argument,
length_error
// out_of_range, range_error,
overflow_error,
// underflow_error

using namespace std;


//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
void
func_level3() throw (bad_alloc)
{
cerr << "Exception bad_alloc in function " << __func__ << "()\n";
throw bad_alloc();
}


//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
void
func_level2() throw (runtime_error)
{
try
{
func_level3();
}
catch(exception& xcptn)
{
ostringstream oss;
oss << "Exception: " << xcptn.what() << " in function " <<
__func__ << "()";
throw runtime_error(oss.str());
}
}

//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
void
func_level1() throw (domain_error)
{
try
{
func_level2();
}
catch(exception& xcptn)
{
ostringstream oss;
oss << "Exception: " << xcptn.what() << " in function " <<
__func__ << "()";
throw domain_error(oss.str());
}
}


//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
main()
{
try
{
func_level1();
}
catch(exception& xcptn)
{
cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ","
<< __LINE__ << endl;
}
}
 
B

BobR

I wrote some sample code (see below) for nested exception throwing
i.e. my catch blocks are throwing exceptions of their own (for
simplicity I used standard exceptions). I am getting some extraneous
characters (St9) in my final output. Can anyone kindly help me
interpret that please?
Thanks, Ramesh

Here's my output:
Exception bad_alloc in function func_level3()
Exception: Exception: Exception: St9bad_alloc in function
func_level2() in function func_level1() at xcptn-test.cpp,74

[ this is only my own theory/logic (i have not read-up on it) ]

GCC(g++) decorates the exception name. I believe that the 'St' stands for
'standard'(or 'standard template'). Someone else thought the '9' is the
number of characters that follow, and that seem to hold true for all I've
seen (in GCC).

What compiler are you using?
( Even though we don't discuss compilers here, it's sometimes helpful to
know which one gave the output.).

If you are deriving from std::exception, see my last post in this thread for
more:
Newsgroups: comp.lang.c++
Sent: Wednesday, May 16, 2007
Subject: exception and polymorphism problem
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top