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 

Reraise error skips finally block?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Non-Technical
View previous topic :: View next topic  
Author Message
Bryce K. Nielsen
Guest





PostPosted: Thu Mar 30, 2006 9:03 pm    Post subject: Reraise error skips finally block? Reply with quote



I was having a conversation with a fellow C# programmer about
try..catch..finally blocks. He was explaining that if you reraise the error
then the finally block will not be executed. For example:

try
{
//do some stuff
}
catch (Exception e)
{
LogError(e);
throw;
}
finally
{
//do something else
}

If there's an exception, that finally block will never be hit. Is this
correct? And if so, how does Delphi For DotNet handle this?

I'm used to programming like this:

MyObject := TMyObject.Create;
try
try
//do some stuff
except
on E:Exception do
begin
LogError(E);
raise E;
end;
finally
FreeAndNil(MyObject);
end;

If this code is run in dotnet, will it still work?

-BKN
Back to top
Peter Morris [Droopy eyes
Guest





PostPosted: Thu Mar 30, 2006 9:03 pm    Post subject: Re: Reraise error skips finally block? Reply with quote



Well, the guy is just simply wrong.....


try
{
throw new Exception("Exception");
}
catch (Exception error)
{
throw;
}
finally
{
MessageBox.Show("Finally was called");
}

Put that in a Button.Click event and run the app without debugging. The
Finally block is executed before the app crashes.



--
Pete
====
Audio compression components, DIB graphics controls, ECO extensions,
FastStrings
http://www.droopyeyes.com

My blog
http://blogs.slcdug.org/petermorris/
Back to top
Trevor de Koekkoek
Guest





PostPosted: Thu Mar 30, 2006 10:03 pm    Post subject: Re: Reraise error skips finally block? Reply with quote



"Bryce K. Nielsen" <bryce (AT) sysonyx (DOT) com> wrote in message
news:442c3de2 (AT) newsgroups (DOT) borland.com...
Quote:
I was having a conversation with a fellow C# programmer about
try..catch..finally blocks. He was explaining that if you reraise the error
then the finally block will not be executed. For example:

try
{
//do some stuff
}
catch (Exception e)
{
LogError(e);
throw;
}
finally
{
//do something else
}

If there's an exception, that finally block will never be hit. Is this
correct? And if so, how does Delphi For DotNet handle this?

This is not correct. The finally block will execute.

-Trevor



--
Trevor de Koekkoek
http://www.aspevia.com
Google Sitemap generator: http://www.sitemagellan.com
Back to top
Mark Anthony Gohara
Guest





PostPosted: Thu Mar 30, 2006 11:03 pm    Post subject: Re: Reraise error skips finally block? Reply with quote

Actually he is right and wrong about this. I ran into this in the 1.0
Framework and it was fixed in the 1.1 version of the Framework.
"Bryce K. Nielsen" <bryce (AT) sysonyx (DOT) com> wrote in message
news:442c3de2 (AT) newsgroups (DOT) borland.com...
Quote:
I was having a conversation with a fellow C# programmer about
try..catch..finally blocks. He was explaining that if you reraise the error
then the finally block will not be executed. For example:

try
{
//do some stuff
}
catch (Exception e)
{
LogError(e);
throw;
}
finally
{
//do something else
}

If there's an exception, that finally block will never be hit. Is this
correct? And if so, how does Delphi For DotNet handle this?

I'm used to programming like this:

MyObject := TMyObject.Create;
try
try
//do some stuff
except
on E:Exception do
begin
LogError(E);
raise E;
end;
finally
FreeAndNil(MyObject);
end;

If this code is run in dotnet, will it still work?

-BKN

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