Board index » cppbuilder » Integrating VCL and C++ exceptions

Integrating VCL and C++ exceptions


2007-09-25 08:58:40 PM
cppbuilder30
I have a feeling that this has been asked before, and if so I apologize,
but I wasn't able to find anything in the archives that really seemed to
help.
I have a small project I'm currently working on that consists of the
IDE-generated WinMain function for VCL projects, a form unit, and a
header file for a template implementation of a stack. Right now the
project is VCL-based, but the stack header could be used later in
non-VCL projects. Consequently, I want exceptions thrown by the stack
functions to be instances of class exception from the standard C++
library rather than VCL Exception instances.
In particular, the Pop() function of the stack looks like this, where
m_Size is the number of elements pushed onto the stack:
template<class T>
T MStack<T>::Pop(void) {
if (m_Size == 0) {
throw range_error("MStack: Pop() called with empty stack");
}
.
.
.
}
In the form unit I have a button and a label (for testing purposes
only). The OnClick() handler for the button is:
void __fastcall TForm1::Button1Click(TObject *Sender) {
MStack<int>* stack1 = new MStack<int>;
try {
//will cause exception since stack is empty
Label1->Caption = stack1->Pop();
}
catch (exception& e) {
::MessageBeep(MB_OK);
}
}
At this point things work fine -- clicking on the button causes a beep
to sound, and the application moves on. However, I don't really want to
have to enclose every single call to Pop() in a try...catch block. What
I'd like to do is have the whole application in a try...catch,
presumably by putting the try...catch in the WinMain function generated
by the IDE. This requires mixing the VCL try...catch's with the C++
try...catch, and I have not been able to get this to work. When I move
the C++ try...catch into WinMain and run the program, there is no beep
and I get a messagebox stating an External Exception has been caught
(I'm guessing this is VCL talking). I have tried enclosing the
Application->Run() statement in a C++ try...catch which is itself in the
VCL try...catch, and I've also tried just adding my own catch's
recognizing C++ exception objects to the list of VCL catch's.
Is it possible to catch C++ exceptions in the WinMain function of a VCL
application by mixing C++ and VCL try...catch blocks? If so, any advice
on how to position the C++ try...catch block would be appreciated.
Thanks -- Steve
 
 

Re:Integrating VCL and C++ exceptions

On Tue, 25 Sep 2007 08:58:40 -0400, salamander1965
< XXXX@XXXXX.COM >wrote:
Quote
but I wasn't able to find anything in the archives that really seemed to
just ask google: translatestandardexceptions
it will explain everything you need in details
Quote
Is it possible to catch C++ exceptions in the WinMain function of a VCL
as regarding to this see forms.pas where vcl handles all exceptions in
the message loop so you won't be able to catch anything thrown from
within some form event handler anyway. in winmain you can only catch
those exceptions raised from the beginning of winmain till
Application.Run so usually I have outermost
try{
...
}catch(Exception &e)
{
VHandleException(&e);
Log(FUNC_NAME,e);
}catch(std:exception &e)
{
VHandleException(&e);
Log(FUNC_NAME,e);
}catch(...)
{
VHandleException(NULL);
Log(FUNC_NAME,"...");
}
in winmain
while everyting else is handled by global exception handlers
--
Vladimir Ulchenko aka vavan
 

Re:Integrating VCL and C++ exceptions

"salamander1965" < XXXX@XXXXX.COM >wrote in message
Quote
Consequently, I want exceptions thrown by the stack functions
to be instances of class exception from the standard C++ library
rather than VCL Exception instances.
Translate C++ Exceptions to VCL Exceptions
www.respower.com/~earlye/programming/TranslateStandardExceptions.htm
Quote
What I'd like to do is have the whole application in a try...catch,
presumably by putting the try...catch in the WinMain function
generated by the IDE.
That is not the correct place to put it. As you have already seen, it won't
work there for what you are asking. Assign a handler to the
TApplication::OnException event instead.
Quote
When I move the C++ try...catch into WinMain and run the
program, there is no beep and I get a messagebox stating
an External Exception has been caught (I'm guessing this is
VCL talking).
That is what is supposed to happen. There are other exception handlers
inside the VCL's RTL. The handler in WinMain() is not used for exceptions
that occur inside of message-based events, such as button clicks. And yes,
an External exception occurs when a C++ exception is caught by one of the
VCL's internal handlers.
Quote
I have tried enclosing the Application->Run() statement in a C++
try...catch
Again, that is the wrong place to put it.
Quote
Is it possible to catch C++ exceptions in the WinMain function
of a VCL application by mixing C++ and VCL try...catch blocks?
The only exceptions that would be caught at that level are those thrown from
the constructors of auto-created forms. Everything else is handled
elsewhere in the RTL.
Gambit
 

{smallsort}