Rethrowing an Exception -- Core Dump

J

jerseycat10

I am trying to overcome the following hurdle. We have several
exception types, derived from a "BaseException" type.

Lets say SpecialException inherits from BaseException.

Lets say a SpecialException is thrown, and handled by try/catch that
is catching "BaseException".

If I retry to throw this exception, I get a core dump.

Here is a short program I wrote to show the error point:

#include <string>
using namespace std;

class BaseException {};

class SpecialException : public BaseException {};

int main()
{
try
{
SpecialException lSpecialException;
throw lSpecialException;
}
catch (BaseException ipBaseException)
{
throw;
}
}


The core dump I get looks like this:

#0 0xb73eac0f in raise () from /lib/tls/libc.so.6
#1 0xb73ec415 in abort () from /lib/tls/libc.so.6
#2 0xb75b1527 in __cxa_call_unexpected () from /usr/lib/libstdc++.so.
5
#3 0xb75b1574 in std::terminate () from /usr/lib/libstdc++.so.5
#4 0xb75b174c in __cxa_rethrow () from /usr/lib/libstdc++.so.5
#5 0x0804869f in main ()
(gdb)


Anything I can do to overcome this hurdle, without explicitly handling
all the sub exception types?

Thanks,
AJ
 
V

Victor Bazarov

I am trying to overcome the following hurdle. We have several
exception types, derived from a "BaseException" type.

Lets say SpecialException inherits from BaseException.

Lets say a SpecialException is thrown, and handled by try/catch that
is catching "BaseException".

If I retry to throw this exception, I get a core dump.

Here is a short program I wrote to show the error point:

#include <string>
using namespace std;

class BaseException {};

class SpecialException : public BaseException {};

int main()
{
try
{
SpecialException lSpecialException;
throw lSpecialException;
}
catch (BaseException ipBaseException)
{
throw;
}
}


The core dump I get looks like this:

#0 0xb73eac0f in raise () from /lib/tls/libc.so.6
#1 0xb73ec415 in abort () from /lib/tls/libc.so.6
#2 0xb75b1527 in __cxa_call_unexpected () from /usr/lib/libstdc++.so.
5
#3 0xb75b1574 in std::terminate () from /usr/lib/libstdc++.so.5
#4 0xb75b174c in __cxa_rethrow () from /usr/lib/libstdc++.so.5
#5 0x0804869f in main ()
(gdb)


Anything I can do to overcome this hurdle, without explicitly handling
all the sub exception types?

What do you explect? If you 'throw;' out of a catch block, you need
another try/catch clauses to catch your exception. You don't have them,
so your 'terminate' is duly called.

V
 
A

aj

I figured out the problem. My test program wasn't accurate anyway, as
it needs an extra level of try/catch to showcase the problem.
 
R

red floyd

I am trying to overcome the following hurdle. We have several
exception types, derived from a "BaseException" type.

Lets say SpecialException inherits from BaseException.

Lets say a SpecialException is thrown, and handled by try/catch that
is catching "BaseException".

If I retry to throw this exception, I get a core dump.

Here is a short program I wrote to show the error point:

#include <string>
using namespace std;

class BaseException {};

class SpecialException : public BaseException {};

int main()
{
try
{
SpecialException lSpecialException;
throw lSpecialException;
}
catch (BaseException ipBaseException)
catch (BaseException& ipBaseException)
{
throw;
}
}

Catch by reference, otherwise you get slicing.
 
L

Lance Diduck

I am trying to overcome the following hurdle. We have several
exception types, derived from a "BaseException" type.

Lets say SpecialException inherits from BaseException.

Lets say a SpecialException is thrown, and handled by try/catch that
is catching "BaseException".

If I retry to throw this exception, I get a core dump.

Here is a short program I wrote to show the error point:

#include <string>
using namespace std;

class BaseException {};

class SpecialException : public BaseException {};

int main()
{
try
{
SpecialException lSpecialException;
throw lSpecialException;
}
catch (BaseException ipBaseException)
{
throw;
}

}

The core dump I get looks like this:

#0 0xb73eac0f in raise () from /lib/tls/libc.so.6
#1 0xb73ec415 in abort () from /lib/tls/libc.so.6
#2 0xb75b1527 in __cxa_call_unexpected () from /usr/lib/libstdc++.so.
5
#3 0xb75b1574 in std::terminate () from /usr/lib/libstdc++.so.5
#4 0xb75b174c in __cxa_rethrow () from /usr/lib/libstdc++.so.5
#5 0x0804869f in main ()
(gdb)

Anything I can do to overcome this hurdle, without explicitly handling
all the sub exception types?

Thanks,
AJ

The reason you are getting a core is because when you are rethrowing
the exception, there is no catch handler. It just propogates out of
main() which is a no-no


Lance
 
R

Ron Natalie

red said:
Catch by reference, otherwise you get slicing.

Yes, but that's not his problem. Rethrow always
rethrows the current exception, not the copy made
in the catch block.
 

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,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top