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 

exception problem

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
delphifan
Guest





PostPosted: Wed Apr 19, 2006 12:33 pm    Post subject: exception problem Reply with quote



Hi, I find the exception mechanism a little puzzling at the moment.for
example:
procedure proc1;
begin
try
//do something;
except
on E:Exception do raise E;
end;
end;
proedure proc2;
try
proc1;
end;
when proc2 calls proc1 and proc1 encouters an exception, proc2 will get that
exception. But it is very strange that the exception that proc2 receives
contains nothing,which means if you use showmessage(e.message) in proc2, you
will get an empty string. Where is the message of the original exception?

We all know that the following code in java is ok:
try {
//do something
catch(Exception e){
throw e;
}
}

By the way, I think delphi really needs some improvement in its exception
part.
in java and c++, try..catch..finally is very popular, but why in delphi, you
could not use try..except and try..finally, but not in one piece.
Hey, members of delphi team, is there anyone of you can explain this.
Thanks.
Back to top
David McCallum
Guest





PostPosted: Wed Apr 19, 2006 1:03 pm    Post subject: Re: exception problem Reply with quote



Should proc2 not be:

procedure proc2;
begin
try
proc1;
except
on E:Exception do ShowMesage(e.message);
end;
end;

David McCallum
Back to top
Iman L Crawford
Guest





PostPosted: Wed Apr 19, 2006 3:03 pm    Post subject: Re: exception problem Reply with quote



"delphifan" <definey2008 (AT) 163 (DOT) com> wrote in news:444622a5
@newsgroups.borland.com:

Quote:
procedure proc1;
begin
try
//do something;
except
on E:Exception do raise E;
end;

The above should be

procedure proc1;
begin
try
//do something;
except
on E:Exception do raise; // no E
end;
end;

--
Iman
Back to top
delphifan
Guest





PostPosted: Wed Apr 19, 2006 3:03 pm    Post subject: Re: exception problem Reply with quote

I've tried this, it is just like what java is doing. But in java it's ok
while in delphi it's not.
"David McCallum" <davidmccallumREMOVE@THISactual-systems.com> 写入消息新闻:444629ad (AT) newsgroups (DOT) borland.com...
Quote:
Should proc2 not be:

procedure proc2;
begin
try
proc1;
except
on E:Exception do ShowMesage(e.message);
end;
end;

David McCallum

Back to top
Rob Kennedy
Guest





PostPosted: Thu Apr 20, 2006 2:03 am    Post subject: Re: exception problem Reply with quote

delphifan wrote:
Quote:
Hi, I find the exception mechanism a little puzzling at the moment.for
example:
procedure proc1;
begin
try
//do something;
except
on E:Exception do raise E;
end;
end;

That catches an exception and then raises it as though it were a totally
new exception. The previous exception sequence is terminated, and the
exception that got caught is freed. Now you're stuck with an invalid
object reference.

To reraise an exception -- to continue the current exception-handling
sequence -- use just "raise" by itself.

Quote:
proedure proc2;
try
proc1;
end;

Huh? You're missing a "finally" or an "except" block. I can't tell which
because I don't know what Proc2 is supposed to do.

Quote:
when proc2 calls proc1 and proc1 encouters an exception, proc2 will get that
exception. But it is very strange that the exception that proc2 receives
contains nothing,which means if you use showmessage(e.message) in proc2, you
will get an empty string. Where is the message of the original exception?

It was destroyed when you handled it in Proc1. You handled the original
exception and then raised a new one. Since Delphi's objects aren't
reference-counted, the RTL didn't know that the caught-and-handled
exception was the same as the new one being raised.

Quote:
We all know that the following code in java is ok:
try {
//do something
catch(Exception e){
throw e;
}
}

It's not equivalent code. In the Delphi code, the ExceptAddr variable
will initially hold the address of the original "raise" statement that
triggered the exception. After the "raise e" command, ExceptAddr will
refer to that line of code, not the original line.

In the Java code, the exception's stack-trace information will continue
to refer to the line where the exception object was first created (which
may or may not be the same as the place where the exception was thrown).

Quote:
By the way, I think delphi really needs some improvement in its exception
part.
in java and c++, try..catch..finally is very popular, but why in delphi, you
could not use try..except and try..finally, but not in one piece.

Because they're totally different constructs. They just happen to both
start with the same reserved word, but they have completely separate uses.

C++ doesn't have try-finally. That's a Microsoft extension, and it's not
tied to the try-except extension or to the built-in try-catch facility.
Neither standard C++ nor Microsoft C++ have try-catch-finally.

--
Rob
Back to top
Bob Dawson
Guest





PostPosted: Thu Apr 20, 2006 2:04 pm    Post subject: Re: exception problem Reply with quote

"delphifan" wrote
Quote:
I've tried this, it is just like what java is doing. But in java it's ok
while in delphi it's not.

The pattern in delphi of

try
[...]
except
on E: Exception do
raise E;
end;

does re-raise the original exception. And the line
on E: Exception do ShowMessage(E.Message);

shows the message of the exception that was caught.

The code you posted, however, is obviously not compilable. Show a working
sample of the problem you think you're seeing.

bobD
Back to top
Iman L Crawford
Guest





PostPosted: Thu Apr 20, 2006 4:03 pm    Post subject: Re: exception problem Reply with quote

"Bob Dawson" <bdawson (AT) idtdna (DOT) com> wrote in news:44478cdb$1
@newsgroups.borland.com:
Quote:
on E: Exception do
raise E;

just raise; putting the E there will cause an AV.

--
Iman
Back to top
Bob Dawson
Guest





PostPosted: Fri Apr 21, 2006 2:04 am    Post subject: Re: exception problem Reply with quote

"Iman L Crawford" wrote
Quote:

just raise; putting the E there will cause an AV.

blush...

procedure TForm1.Catch2;
begin
try
RaiseOne;
except
// clean up
raise;
end;
end;

procedure TForm1.Catch3;
begin
try
RaiseOne;
except
on e:Exception do
begin
//cleanup
raise;
end;
end;
end;
Back to top
Iman L Crawford
Guest





PostPosted: Fri Apr 21, 2006 3:04 pm    Post subject: Re: exception problem Reply with quote

"Bob Dawson" <RBDawson (AT) prodigy (DOT) net> wrote in news:44482f64$1
@newsgroups.borland.com:
Quote:
blush...

don't feel bad, I had to read the help to remind me how to do it correctly.

--
Iman
Back to top
delphifan
Guest





PostPosted: Sat Apr 22, 2006 2:03 am    Post subject: Re: exception problem Reply with quote

Hi,Rob
Thank you for your reply. I've tried it, it did work as you said.
Then a java try-catch-finally is equivalent to a delphi
try-try-except-finally block, am I right?
just like this:
try{
//do something
}
catch(Exception e){//exception hander}
finally{
//cleaning procedure
}

can be replaced by the following delphi code:
try
try
//do something
except
on E:Exception do //exception handler
end;
finally
//cleaning procedure
end;

Am I right?
Back to top
Rob Kennedy
Guest





PostPosted: Sat Apr 22, 2006 5:03 pm    Post subject: Re: exception problem Reply with quote

delphifan wrote:
Quote:
Thank you for your reply. I've tried it, it did work as you said.
Then a java try-catch-finally is equivalent to a delphi
try-try-except-finally block, am I right?

Yes, those are the same.

--
Rob
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design 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.