Board index » cppbuilder » assert and OnException

assert and OnException


2003-07-22 06:06:17 PM
cppbuilder94
Hello all,
assert() is often used in my app. My problem is that I want to
handle this exception differently from the others, but I cannot
catch it because it is not a VCL Exception, so using OnException
of TApplicationEvents is inappropriate.
What can I do about it? The help says that EAssertionFailed gets
called when using Assert(). Assert() (with a capital A) itself
however is not mentioned in help and I cannot find it.
Well, thinking it over... actually the exeception raised by
assert() is just one example where a C++ exception should be
caught by VCL. So I better not look for an Assert with a capital
A (wouldn't make much sense anyhow because not everything in my
app is written by me), but find out how to catch C++ exceptions.
Can someone tell me how to install an OnException handler in my
app that handles both VCL and C++ exceptions?
TIA
Thorsten.
 
 

Re:assert and OnException

Thorsten Kettner wrote:
Quote
Hello all,
assert() is often used in my app. My problem is that I want to
handle this exception differently from the others, but I cannot
catch it because it is not a VCL Exception, so using OnException
of TApplicationEvents is inappropriate.
You cannot handle it because it is not an exception. Instead, it will
call terminate() and abort your application immediately after displaying
its message. At least, it will in debug builds <g>
Quote
What can I do about it?
Throw an exception instead of using assert. std::logic_error might make
a reasonable base class (although I am never comfortable writing code
that carries on working after identifying a bug. Instead, I would
rather fix the bug and assert forces me to do that)
If you want the no-asserts-in-release-build behaviour you could always
test for NDEBUG yourself, or write you own replacement for the assert
macro that works the way you want.
--
AlisdairM
Team Thai Kingdom
 

Re:assert and OnException

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"Thorsten Kettner" < XXXX@XXXXX.COM >wrote in message
news:3f1d0c99$ XXXX@XXXXX.COM ...

>Can someone tell me how to install an OnException
>handler in my app that handles both VCL and C++ exceptions?
The VCL only understands VCL exceptions. However, unhandled
C++ exceptions are typically translated into an EEFFACE
EExternalException.
Yes I know them too well. It's annoying to see that exceptions
are thrown to give me information about what happened and VCL
"translates" them to useless EEFFACE exceptions.
Quote
Alternatively, look back through the archives for these groups
for the last couple of weeks. Someone posted a link to an
article that explains how to translate C++ exceptions into
custom VCL Exceptions. I don't recall where I saw it or who
posted it, and I didn't keep the URL. Otherwise I'd simply
tell you where to go, sorry.
I don't find anything helpful :(
Maybe you can explain why exceptions never get through to
WinMain. I tried to catch stl exceptions there, but the catch
blocks there seem to be waste, they are never executed!
I hope you can help me getting this solved Gambit (or anybody
else, I appreciate any help I can get!). We are using OTL and
other 3rd party libs and I need to catch the exceptions! (I am
going to change assert.h that assert throws an exception too).
The question is: How to catch C++ exceptions (and maybe even
exceptions thrown with throw("sometext")?
 

{smallsort}

Re:assert and OnException

Thorsten Kettner wrote:
Quote
Maybe you can explain why exceptions never get through to
WinMain. I tried to catch stl exceptions there, but the catch
blocks there seem to be waste, they are never executed!
The following link to Early Ehlinger's web site is what Gambit was
referring to. The thread was in the .language group.
www.respower.com/~earlye/programming/TranslateStandardExceptions.htm
The reason exceptions to not get through to WinMain is that is not where
the VCL handles them. Instead, they are caught and translated in a
try/except block wrapping the main message loop in the TApplication
object (where we have no access to it)
Quote
The question is: How to catch C++ exceptions (and maybe even
exceptions thrown with throw("sometext")?
Early demonstrates a very neat hack to avoid worrying about the problem,
and if I had a lot of code to handle now which was currently unguarded
(for non-VCL exceptions) I would take that route as the alternative is a
LOT of work.
If you opt for the future-proof version, you MUST wrap every single
event handler on every single form with a
try{}
catch( const std::exception &e)
{}
handler.
In our case, we treat logic_error and runtime_error separately, and also
cover the catch(...) case just to be sure!
In this handler you can then display your message to the user, often
e.what().
In addition, especially if you write components, you must wrap every
single virtual function override with a similar block. In these cases,
it is often not appropriate to show a message to the user so I rethrow a
VCL exception instead (using e.what() as the message) and allow the VCL
framework to do whatever it was going to do anyway.
Extra-special note for components: You really don't want non-VCL
exceptions escaping into the IDE.
Also remember every event handler you add to a component is an
opportunity for users of that component to throw C++ exceptions as well.
This is a huge ammount of work that would be unnecessary if the VCL
wrapped C++ exceptions better. Early has shown the way but until
Borland see fit to adopt something similar, there is no safe substitute
for the 'boilerplate' coding.
[I would take Early's solution as, even if I could not maintain it
myself is necessary for a future version, the only result is that I have
to do all the work I had avoided until then. So large potential gain,
minimal downside. OTOH, we already have the wrappers in place so we're
not about to throw them all away!!]
--
AlisdairM
Team Thai Kingdom
 

Re:assert and OnException

Thorsten Kettner wrote:
Quote
Yes Borland should fix that. A C++ System must be able to
handle C++ standard exceptions. If the VCL is not capable of
doing so, it should let them pass, so we can catch them in
WinMain, rather than eat them and pass us a cripple exception.
Note that catching them in WinMain is WAY too late, unless you want your
application to quit after the first non-VCL exception. These exceptions
need to be handled in the main message loop.
--
AlisdairM
Team Thai Kingdom
 

Re:assert and OnException

"Thorsten Kettner" < XXXX@XXXXX.COM >wrote in message
Quote
Yes I know them too well. It's annoying to see that
exceptions are thrown to give me information about
what happened and VCL "translates" them to useless
EEFFACE exceptions.
I recently read an article that showed a simple way of using the VCL's
undocumented ExceptProc handler to translate individual C++ exceptions
(including std::exception and derivitives) into more meaningful VCL
exceptions relatively easily, so EEFFACE was not thrown anymore.
Unfortunately, I do not remember where I saw the article :-(
Gambit
 

Re:assert and OnException

"Alisdair Meredith" < XXXX@XXXXX.COM >wrote in message
Quote
The following link to Early Ehlinger's web site
is what Gambit was referring to.
Yup, that is the one, thanks.
Gambit