BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using exception object outside try-catch block

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language)
View previous topic :: View next topic  
Author Message
Pedja
Guest





PostPosted: Tue Jul 08, 2003 6:55 am    Post subject: Using exception object outside try-catch block Reply with quote



Hello.

I need to do something like this:

Exception *pe = NULL;

try
{
....
}
catch( Exception &e )
{
....
pe = &e;
....
}
catch( ... )
{
....
}

if ( pe )
{
// use pe
}

In this example, I presume that 'pe' will be dangling pointer outside
try-catch block (if initialized inside).

If I use:

pe = new Exception( e );

instead of:

pe = &e;

this will not do the job, since 'e' could be object of the class
derived from Exception. In that case I couldn't access 'additional'
functionality.

Am I missing something obvious here?
Does anybody have any advise?

Thanks.
- Pedja.
Back to top
JKroetch
Guest





PostPosted: Wed Aug 13, 2003 4:33 pm    Post subject: Re: Using exception object outside try-catch block Reply with quote



"Eivind Midtgård" <see.signature (AT) somewhere (DOT) invalid> wrote

Quote:

You want to use the exception outside of the try/catch.
Then the thrown exception must be allocated on the heap,
i.e. you must use "throw new Exception " instead
of "throw Exception". Your catch should
be modified to read "catch (Exception* e)".

(Let me add that it is generally no good idea to
mix references and pointers.)



EEEEK! Definitely don't start throwing around allocated exceptions!
You'll be leaking memory like a sieve (cause you'll have to delete
them at all points, etc.) Even with Smart Wrappers to deallocate the
memory, this is not a good idea.

My guess is you're using the exception to pass some kind of
information along? In that case, I'd create a NEW class that takes the
exception in the constructor and gains the pertinent information from
it. You'd end up with this:

(Pseudo-code)
///////////////////////////////////////////
class MyExceptionInfo {
public:
MyExceptionInfo(MyException& e) {
// Copies the data
}
};


/// in code
MyExceptionInfo* pInfo = NULL;
try {
// Throws MyException
GetData();
}
catch (MyException &e) {
pInfo = new MyExceptionInfo(e);
}

if (pInfo != NULL) {
// Exception was thrown!

// Use data!

// Ensure I clean up!
delete pInfo;
pInfo = NULL;
}
///////////////////////////////////////////


Much safer & cleaner. DO NOT DO NOT DO NOT "throw new MyException();"
You've been warned. :)

John

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.