Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Assertion not shown when in try...except

31 views
Skip to first unread message

Jens Berke

unread,
Feb 28, 2001, 10:01:24 AM2/28/01
to
Why does the following code not show the assertion when I switch off "Stop
on Delphi-exceptions" under "Tools - Debug-Options"? I know an assertions
raises an EAssertionFailure-exception and therefore it's not shown.But
that's not what assertions are meant to be: they should come anyway as long
as I have the compiler-option "Assertions" set.

begin
try
assert(false); // not shown when "Stop on Delphi-exceptions" is off,
even when "Asssertions" is on
except
end;

How can I assure that I _always_ get assertions shown, even when "Stop on
Delphi-exceptions" if off and there's a try except around?

Jens


Howard Moon

unread,
Feb 28, 2001, 10:07:02 AM2/28/01
to
Is your code sample incomplete? It looks like you're trapping the
exception (which is what the assertion causes), and then abandoning it.
Thus, no message. Try this to prove my point:

try
assert(false);
except
on E:Exception do showmessage(E.message);
end;

-Howard


Jens Berke

unread,
Feb 28, 2001, 11:00:25 AM2/28/01
to
> Is your code sample incomplete?
it is, I thought I post it as simple as possible. Here's are more detailed
example. Hope that clears what I mean:

procedure bla;
var
ThingsAreOK: boolean;
i : integer;
begin
try
i := DoSomeStuff; // this could cause an ESomeStuffException
ThingsAreOK := Calculate(i);
assert(ThingsAreOK, 'Things are not OK!!!');
except
On E:ESomeStuffException do
ShowMessage('Doing some stuff failed!');
end;
end;


Howard Moon

unread,
Feb 28, 2001, 11:18:46 AM2/28/01
to
Now I'm more confused...If the DoSomeStuff should raise an exception (and
you don't say whether it did or not in testing), then the assert line will
never be reached, because the code will jump from the DoSomeStuff to the
except clause.
I checked the Help, and experimented with a simple app using the sample I
gave you, and found that Delphi's component libraries handle the exception
Assert raises, so your try...except will not be able to trap it. If you
need to be able to trap exceptions in your production code, I would
recommend using and if..then statement and raising your own exception within
that. That way, you won't have production code with assertions turned on,
and you'll be able to trap it easily.

-Howard


Ralph Friedman (TeamB)

unread,
Feb 28, 2001, 11:40:33 AM2/28/01
to
Jens,

in article <97j7bq$ai...@bornews.inprise.com>, you wrote:


it's behaving as designed. The Assertion triggers an exception that you are not
handling. If you don't have "Stop on Delphi Exceptions" checked you won't see
it at all.

--
Regards
Ralph (TeamB)
===

Jonas Malmsten

unread,
Feb 28, 2001, 1:00:26 PM2/28/01
to
Try this (added one line):

procedure bla;
var
ThingsAreOK: boolean;
i : integer;
begin
try
i := DoSomeStuff; // this could cause an ESomeStuffException
ThingsAreOK := Calculate(i);
assert(ThingsAreOK, 'Things are not OK!!!');
except
On E:ESomeStuffException do
ShowMessage('Doing some stuff failed!');

else raise;
end;
end;

//Jonas

Peter Below (TeamB)

unread,
Feb 28, 2001, 1:28:57 PM2/28/01
to
In article <97j7bq$ai...@bornews.inprise.com>, Jens Berke wrote:
> it is, I thought I post it as simple as possible. Here's are more detailed
> example. Hope that clears what I mean:
>
> try
> i := DoSomeStuff; // this could cause an ESomeStuffException
> ThingsAreOK := Calculate(i);
> assert(ThingsAreOK, 'Things are not OK!!!');
> except
> On E:ESomeStuffException do
> ShowMessage('Doing some stuff failed!');
> end;

Change the except to

> except
> On E:ESomeStuffException do
> ShowMessage('Doing some stuff failed!')

Else
raise
> end;


Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Igor Ivanov

unread,
Mar 1, 2001, 1:04:00 AM3/1/01
to

> > except
> > On E:ESomeStuffException do
> > ShowMessage('Doing some stuff failed!');
> > end;
>
> Change the except to
>
> > except
> > On E:ESomeStuffException do
> > ShowMessage('Doing some stuff failed!')
> Else
> raise
> > end;

According to help, both should behave in exactly the same way. In a small
test I tried this was indeed so.

If you mean that sometimes they behave differently, there's a compiler bug
here.


Jens Berke

unread,
Mar 1, 2001, 3:28:25 AM3/1/01
to
Uhm, I'm really sorry, but I think I got it all wrong yesterday. I don't
know why and how it happened, but the assertion wasn't shown - though it
should - which seems te be my fault somehow. The code I posted does exactly
what I expect from it now - even without the suggested "raise" at the end of
the "except"-block - and so my question has become obsolete.

Thanks for all the answers.

Jens


Jens Berke

unread,
Mar 1, 2001, 5:23:25 AM3/1/01
to
I just found out how assertions are really handled and what the problem was
yesterday. Here are two examples to show it:

This code shows the assertion, no matter if "Stop on Delphi-Exceptions" is
on or off:

procedure TForm1.Button1Click(Sender: TObject);
begin
try
assert(false, 'ASSERTION!!!!!');
except
on E:EAssertionFailed do // works with any other Exception-Type as
well
raise;
end;
end;

This code DOESN'Tt show the assertion, when the "Stop on
Delphi-Exceptions"is OFF:

procedure TForm1.Button1Click(Sender: TObject);
begin
try
assert(false, 'ASSERTION!!!!!');
except
MessageBeep(0); // or any other code without ON E:... DO and without
RAISE
end;
end;

Conclusion: if you want to be sure that you see all assertions fired, use
code like the one in the first example. If you work in a team you never
know all hidden assertions in the methods written by others - if you use
these methods in a try-block it seems wise to use "on E:EAssertionFailed..."
for ALL try... except blocks you write!

Jens


Antonios Christofides

unread,
Mar 1, 2001, 10:38:16 AM3/1/01
to
Jens Berke wrote:

> Conclusion: if you want to be sure that you see all assertions fired, use
> code like the one in the first example.

I think you have the wrong conclusion. The more general one, and the correct
one, is: make sure ALL exceptions are handled. Thus, if you have

try
...
except
on SomeException do something;
on SomeOtherException do something else;
else raise; // This line shouldn't be missed
end;

(You may ommit "else raise" if you have trapped Exception).

Igor Ivanov

unread,
Mar 1, 2001, 1:26:23 PM3/1/01
to
> else raise; // This line shouldn't be missed

Do you have a specific reason for this? See my reply to Peter Below.

Antonios Christofides

unread,
Mar 2, 2001, 6:07:52 AM3/2/01
to
Igor Ivanov wrote:

> > else raise; // This line shouldn't be missed
>
> Do you have a specific reason for this? See my reply to Peter Below.

Goodness, you're right! It's documented and works as you said.

So let's go back to Jens Berke's conclusion:

> This code DOESN'Tt show the assertion, when the "Stop on
> Delphi-Exceptions"is OFF:
>
> procedure TForm1.Button1Click(Sender: TObject);
> begin
> try
> assert(false, 'ASSERTION!!!!!');
> except
> MessageBeep(0);

> end;
> end;
>

If you don't have any "on" in the except block, then the except block
itself is considered to be an exception handler. So control does pass to
the except block, so MessageBeep(0) is executed.

After that, the exception is considered handled, so it is not raised any
more, unless you explicitly raise it.

try
assert(false);
except
MessageBeep(0);
raise;
end;

This is probably what you want.

0 new messages