Try...except problem

delphi package - automated exception handling
Post Reply
dcumpian
Posts: 8
Joined: Mon Nov 28, 2005 6:58 pm

Try...except problem

Post by dcumpian »

Hello,

I have database code in a thread that is wrapped in a try...except block. With madExcept disabled, the try...except code works as it is supposed to. With madExcept enabled, the madExcept dialog appears and the except code never executes.

The code basically is an insert into a database:

try
Synchronize(InsertData);
except
// code to check database for errors...

end;

procedure InsertData;
begin
Query.ExecSQL;
end;

What can I do to get around this issue?

Regards,
Dan
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

The problem is that when there's an exception in the synchronized method, the exception occurred in the context of the main thread, while the try..except statement is in the context of the secondary thread. If you now check out the callstack of the secondary thread, it doesn't help at all, because the crash occurred in the main thread.

Because of this problem madExcept hooks into TThread.Synchronize and catches exceptions in that thread where they are raised (= in the main thread). This is the only way to make sure that you get a proper helpful report with useful stack traces.

How can you work around the problem now? Can't you move the try..except statement into the "InsertData" method? Then everything will work fine again.

Basically madExcept has its own "try..except" block around synchronized method calls.
dcumpian
Posts: 8
Joined: Mon Nov 28, 2005 6:58 pm

Post by dcumpian »

Got it. OK, moving the try...except block solves the problem, and I understand completely why madExcept works this way. It wasn't clear at the time though...

You might consider adding something to your FAQ about this :wink:

Regards,
Dan
Post Reply