Delphi exception handling problem with multiple exception handling blocks

I am using Delphi Pro 6 on Windows XP with FastMM 4.92 and JEDI JVCL 3.0. Given the code below, I have the following problem: only the first exception handling block receives a valid instance of E. Other blocks correspond to the rule with the exception raising class, but E is not assigned (nil).

For example, given the current order of exception handling blocks, when I raise E1, the block for E1 matches, and E is a valid instance of the object. However, if I try to raise E2, this block will match, but E is not assigned (nil). If I moved the capture block E2 to the top of the order and raise E1, then when the block E1 corresponds to E, it is no longer assigned. With this new ordering, if I raise E2, E is correctly assigned when it was not, when the E2 block was not the first block in the order. Note. I tried this case with a bare-bones project consisting of just one Delphi form.

Am I doing something really stupid here or is something really wrong?

Thanks Robert

type E1 = class(EAbort) end; E2 = class(EAbort) end; procedure TForm1.Button1Click(Sender: TObject); begin try raise E1.Create('hello'); except On E: E1 do begin OutputDebugString('E1'); end; On E: E2 do begin OutputDebugString('E2'); end; On E: Exception do begin OutputDebugString('E(all)'); end; end; // try() end; 
+7
null exception try-catch delphi except
source share
1 answer

If I am right, the behavior that you see is witnessed by evaluating E under the debugger (I got the same behavior by checking this in the BDS 2006 debugger).

This is a character resolution error in the debugger, but does not affect runtime behavior.

If debugging behavior is important, simply rename the exception handler variables so that the debugger does not have (potential) ambiguities that need to be addressed:

 On E1: E1 do begin OutputDebugString('E1'); end; On E2: E2 do begin OutputDebugString('E2'); end; On Ex: Exception do begin OutputDebugString('E(all)'); end; 
+8
source share

All Articles