By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

Howto rethrow an exception outside a catch block?

P: n/a

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?

Aug 8 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a

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.

Aug 8 '07 #2

P: n/a
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.

Aug 8 '07 #3

P: n/a
On 2007-08-08 09:46:53 -0400, re**********@yahoo.de said:
>
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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 8 '07 #4

P: n/a
Hello,

re**********@yahoo.de wrote:
>
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

Aug 9 '07 #5

P: n/a
Hello,

re**********@yahoo.de wrote:
>
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

Aug 9 '07 #6

P: n/a
Hello,

re**********@yahoo.de wrote:
>
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

Aug 9 '07 #7

P: n/a
Hello,

re**********@yahoo.de wrote:
>
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

Aug 9 '07 #8

P: n/a
Hello,

re**********@yahoo.de wrote:
>
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

Aug 9 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.