473,698 Members | 2,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 CMemoryExceptio n();
}
catch (CException * pEc) // Catch all exceptions derived from
CException.
{
m_pEc = pEc; // Store Exception.
}

.....

CMemoryExceptio n 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 (CMemoryExcepti on * 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 (CMemoryExcepti on) - even if I don't know the
type beforehand?

Aug 8 '07 #1
8 3396

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**********@ya hoo.de said:
>
How can I rethrow the exception, so that it will be thrown with the
right type information (CMemoryExcepti on) - 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__e xception();
}
std::rethrow_ex ception(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**********@ya hoo.de wrote:
>
try
{
CException * pEc = m_pEc;
m_pEc = NULL; // Clear variable.
throw pEc;
}
catch (CMemoryExcepti on * 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 (CMemoryExcepti on) - 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**********@ya hoo.de wrote:
>
try
{
CException * pEc = m_pEc;
m_pEc = NULL; // Clear variable.
throw pEc;
}
catch (CMemoryExcepti on * 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 (CMemoryExcepti on) - 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**********@ya hoo.de wrote:
>
try
{
CException * pEc = m_pEc;
m_pEc = NULL; // Clear variable.
throw pEc;
}
catch (CMemoryExcepti on * 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 (CMemoryExcepti on) - 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**********@ya hoo.de wrote:
>
try
{
CException * pEc = m_pEc;
m_pEc = NULL; // Clear variable.
throw pEc;
}
catch (CMemoryExcepti on * 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 (CMemoryExcepti on) - 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**********@ya hoo.de wrote:
>
try
{
CException * pEc = m_pEc;
m_pEc = NULL; // Clear variable.
throw pEc;
}
catch (CMemoryExcepti on * 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 (CMemoryExcepti on) - 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
2377
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
6000
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
5
11018
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
5834
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
1874
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 (ContextUtil.IsInTransaction)ContextUtil.SetAbort(); ReportError objError = new ReportError(); objError.PersistError(e,null,"DBWrite:executeStoredProcedureReturnXML","Conn ectionString = " + m_sqlConnectionString + " : blnClearFlag=" + blnClearFlag
1
1971
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 contains the thousand of classes and I do not want to use the Try Catch block in each class. Because it is logically similar to GOTO statement. I want to prevent exception from being swallowed.
6
1625
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: Company.Controls.Web.LookupControls.EmployeeLookupControl Now I have a Employee class: Company.Business.Employee
4
2037
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 like this: Try TryToDoIt() Catch e as Exception LogTheError(e)
24
2369
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(); --------------------------------------------------------- ??? --------------------------------------------------------- MyClass::MyClass() {
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9027
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.