| View previous topic :: View next topic |
| Author |
Message |
Bryce K. Nielsen Guest
|
Posted: Thu Mar 30, 2006 9:03 pm Post subject: Reraise error skips finally block? |
|
|
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
|
Posted: Thu Mar 30, 2006 9:03 pm Post subject: Re: Reraise error skips finally block? |
|
|
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
|
Posted: Thu Mar 30, 2006 10:03 pm Post subject: Re: Reraise error skips finally block? |
|
|
"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
|
Posted: Thu Mar 30, 2006 11:03 pm Post subject: Re: Reraise error skips finally block? |
|
|
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 |
|
 |
|