Howto rethrow an exception outside a catch block?

R

reuce-google

Hi folks,

I want to store an exception and rethrow it later:

CException m_pEc = NULL; // Class variable.

try
{
throw new CMemoryException();
}
catch (CException * pEc) // Catch all exceptions derived from
CException.
{
m_pEc = pEc; // Store Exception.
}

.....

CMemoryException is derived from CException. But if I now throw m_pEc
the Exception raised will be caught merely by CException catch blocks,
unfortunately:

try
{
CException * pEc = m_pEc;
m_pEc = NULL; // Clear variable.
throw pEc;
}
catch (CMemoryException * pEc)
{
// Won't catch the exception.
}
catch (CException * pEc)
{
// Will catch the exception.
}

.....

How can I rethrow the exception, so that it will be thrown with the
right type information (CMemoryException) - even if I don't know the
type beforehand?
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi folks,

I want to store an exception and rethrow it later:

CException m_pEc = NULL; // Class variable.

try
{
throw new CMemoryException();
}
catch (CException * pEc) // Catch all exceptions derived from
CException.
{
m_pEc = pEc; // Store Exception.
}

....

CMemoryException is derived from CException. But if I now throw m_pEc
the Exception raised will be caught merely by CException catch blocks,
unfortunately:

try
{
CException * pEc = m_pEc;
m_pEc = NULL; // Clear variable.
throw pEc;
}
catch (CMemoryException * pEc)
{
// Won't catch the exception.
}
catch (CException * pEc)
{
// Will catch the exception.
}

....

How can I rethrow the exception, so that it will be thrown with the
right type information (CMemoryException) - even if I don't know the
type beforehand?

In the catch block you can do

throw;

Note that it's not a good idea to throw pointers to dynamically
allocated memory, for it may be caught by a catch(...), and who's then
to deallocate?

Better use standard C++ exceptions.
 
R

reuce-google

Hello Alf,
In the catch block you can do

throw;

if you had read my subject carefully... ;-)
Note that it's not a good idea to throw pointers to dynamically
allocated memory, for it may be caught by a catch(...), and who's then
to deallocate?

Better use standard C++ exceptions.

Thanks for that advice. It's me who deallocates, so I can deal with
that.

Ron.
 
R

reuce-google

Better use standard C++ exceptions.
Thanks for that advice. It's me who deallocates, so I can deal with
that.

Sorry, I didn't read your answer carefully. But it is still not the
point.

Ron.
 
P

Pete Becker

How can I rethrow the exception, so that it will be thrown with the
right type information (CMemoryException) - even if I don't know the
type beforehand?

You can't. The next version of the C++ standard will provide a
mechanism for doing this:

std::exception_ptr ptr = 0;
catch(whatever)
{
ptr = std::current__exception();
}
std::rethrow_exception(ptr);

There are no compilers that support this today.
 
B

Bernd Strieder

Hello,

try
{
CException * pEc = m_pEc;
m_pEc = NULL; // Clear variable.
throw pEc;
}
catch (CMemoryException * pEc)
{
// Won't catch the exception.
}
catch (CException * pEc)
{
// Will catch the exception.
}

....

How can I rethrow the exception, so that it will be thrown with the
right type information (CMemoryException) - even if I don't know the
type beforehand?

Add a virtual rethrow method to the base class, which is overwritten in
all derived classes to throw itself, i.e. with the right type. Then
call that method.

Bernd Strieder
 
A

Alf P. Steinbach

* Bernd Strieder:
Hello,



Add a virtual rethrow method to the base class, which is overwritten in
all derived classes to throw itself, i.e. with the right type. Then
call that method.

The OP seems to be using MFC (Microsoft Foundation Classes) exceptions;
if so, then no control.
 

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,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top