473,320 Members | 1,965 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,320 software developers and data experts.

Howto rethrow an exception outside a catch block?


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
8 3339

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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: cody | last post by:
public DateTime Value { get { try { return new DateTime(int.Parse(tbYear.Text), int.Parse(tbMonth.Text), int.Parse(tbDay.Text)); } catch (FormatException)
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
5
by: A | last post by:
Hi, I'm having some difficulty understanding the semantics of the rethrow keyword. Consider the following code: int main(){ try{ ... } catch(SomeException &SE){
8
by: Taylor | last post by:
I've run in to code with this pattern: try { // do some potentially bad stuff } catch(System.Exception ex) { throw ex; }
5
by: Kevin Jackson | last post by:
In the following code snippet, will the finally block be executed when the throw is executed in the catch block???? I'm assuming it will. catch (Exception e) { // if...
1
by: Noor | last post by:
Hi all, I am trying to catch all types of exceptions from a app regardless of whether it is in debugger mode( VS development environment) or run the.exe file outside the IDE. My App...
6
by: cmay | last post by:
I'm just looking for some other opinions on this. I have a control that does employee lookups, auto complete of the name etc. Let say that this control is:...
4
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks...
24
by: Chameleon | last post by:
Is there a possibility to create memory leak, the code below if I run the line: --------------------------------------------------------- MyClass cl = new MyClass();...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.