chronozphere
12-05-2011, 09:38 PM
Hey guys,
At this moment, I'm looking at threads more closely and it seemed to me that before you deploy threads at a large scale in your application, you have to think about how to trap and handle exceptions.
First, I'm not a fan of this:
procedure TSubsystemThread.Execute;
begin
try
//do your stuff here
except //eat all exceptions
end;
end;
IMHO, this is one of the most awfull things you can do to yourself (programming-wise that is ;) ), as you make it really hard to find errors later on.
So I was looking at a better solution and found this (2nd example on page):
http://edn.embarcadero.com/article/10452
What strikes me here, is that to make the mechanism work, you make a call to ExceptObject(), which seems to access global exception information. I might be nitpicking here, but is this thread-safe?
Having said that, isn't the following solution alot better?
procedure TMyThread.Execute;
begin
FException := nil;
try
// raise an Exception
raise Exception.Create('I raised an exception');
except
on E: Exception do
HandleException(E); //Pass exception object to handler, and then to form
end;
end;
procedure TMyThread.HandleException(ExceptionObject: Exception);
begin
FException := ExceptionObject;
try
// Don't show EAbort messages
if not (FException is EAbort) then
Synchronize(DoHandleException);
finally
FException := nil;
end;
end;
Does anyone have experience with this? Seems like an obligatory question every programmer has to answer before starting to get serieus with threads.
Thanks!
At this moment, I'm looking at threads more closely and it seemed to me that before you deploy threads at a large scale in your application, you have to think about how to trap and handle exceptions.
First, I'm not a fan of this:
procedure TSubsystemThread.Execute;
begin
try
//do your stuff here
except //eat all exceptions
end;
end;
IMHO, this is one of the most awfull things you can do to yourself (programming-wise that is ;) ), as you make it really hard to find errors later on.
So I was looking at a better solution and found this (2nd example on page):
http://edn.embarcadero.com/article/10452
What strikes me here, is that to make the mechanism work, you make a call to ExceptObject(), which seems to access global exception information. I might be nitpicking here, but is this thread-safe?
Having said that, isn't the following solution alot better?
procedure TMyThread.Execute;
begin
FException := nil;
try
// raise an Exception
raise Exception.Create('I raised an exception');
except
on E: Exception do
HandleException(E); //Pass exception object to handler, and then to form
end;
end;
procedure TMyThread.HandleException(ExceptionObject: Exception);
begin
FException := ExceptionObject;
try
// Don't show EAbort messages
if not (FException is EAbort) then
Synchronize(DoHandleException);
finally
FException := nil;
end;
end;
Does anyone have experience with this? Seems like an obligatory question every programmer has to answer before starting to get serieus with threads.
Thanks!