Update history: No.2
Update time: 2001-10-21 02:42
Updated by: Musicwind®
Update note: Modified format.
Update history: No.1
Update time: 2001-10-19 21:15
Updated by: Musicwind®
Update Notes: Created.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~
Preface: I have been immersed in Delphi for more than three years, but every time I read Delphi 's help documentation, I still gain a lot, so I can't help but sigh at how broad Delphi is! While I feel ashamed, I will sort out some of my experiences and gains, and share my encouragement with those of you who are as arrogant and less knowledgeable as me.
1. What is a silent exception? (Why not Silence of the Lambs? ;- ))
Silent exceptions, that is, Slient Exceptions , refer to the exception type that does not cause annoying message prompts by default: EAbort . In Object Pascal, the exception class EAbort is the ancestor class of all silent exception classes (and EAbort inherits Exception). Raising an EAbort will cause an execution module to stop until the outermost exception handling module intercepts it, but will not cause a message box with a red stop sign to appear. Refer to the following code:
try
ShowMessage('Hello1');
Raise EAbort.Create('Abort it');
ShowMessage('Hello2');
except
on E: Exception do
showmessage('On Exception');
end;
The execution result displays two message boxes, one is "Hello1" and the other is "On Exception". This shows that EAbort does work, because it skips the "ShowMessage('Hello2')" statement; at the same time, the "Abort it" message box does not appear, which also confirms the feature of the EAbort exception class that no dialog box appears. (This is also true during design); and, the message box "On Exception" indicates that although EAbort may be an anomaly different from ordinary exceptions, this does not prevent us from using the old Try-Except statement to catch it.
2. Why use EAbort ?
EAbort is useful in certain situations. For example, when we need to terminate an operation but don't want users to notice it (we don't want them to see the default exception message box). Of course, to achieve the same effect, you can also use ordinary exceptions (for example, use the Try-Except sentence pattern, put the code in the Try section, raise an exception if you need to terminate the operation, and do not write any code in the Except section), But none of this is as simple and direct as using EAbort.
3. Is there anything simpler? --Using the Abort process
Abort, a process defined in the SysUtils unit, allows us to use EAbort conveniently. View the implementation source code of Abort:
PRocedure Abort;
function ReturnAddr: Pointer;
asm
MOV EAX,[EBP + 4]
end;
begin
raise EAbort.Create(SOperationAborted) at ReturnAddr;
end;
SOperationAborted here is usually "Operation aborted".
4. Lift the veil - implementation principle
Maybe you, like me, are curious about why EAbort is silent. What has Delphi done to EAbort internally? Let's find out together.
Open a new project, click Find in Files, enter the "EAbort" keyword, then select the Search in Directories radio button, and set the File Mask edit box in Search Directory Options to the directory name where the Delphi source code is located (such as mine Yes: D:Program FilesBorlandDelphi6Source), and don’t forget to check Include SubDirectory. Finally, click "OK" to start the search.
As a result, we found that there are as many as 17 places in the Delphi source code that are related to EAbort. Except for the two declarations of EAbort and some comment statements in the Sysutils unit, we found that most of the code is similar to:
if ExceptObject is EAbort then
as well as:
if not (E is EAbort) then
etc.
All of them have special treatment for EAbort based on RTTI - it turns out that the implementation of EAbort is that simple!
The two units worthy of attention are: Forms (the Linux version is QForms) and AppEvnts. It is easier to find the answer to the question in the code of the former. See Delphi source code:
procedure Tapplication.HandleException(Sender: TObject);
begin
if GetCapture <> 0 then SendMessage(GetCapture, WM_CANCELMODE, 0, 0);
if ExceptObject is Exception then
begin
if not (ExceptObject is EAbort) then
if Assigned(FOnException) then
FOnException(Sender, Exception(ExceptObject))
else
ShowException(Exception(ExceptObject));
end else
SysUtils.ShowException(ExceptObject, ExceptAddr);
end;
5. The difference between Abort , Break and Exit
Abort, Break and Exit are somewhat similar, yet very different. Break is used to break out of a loop in a loop statement. Exit is used to jump out of the currently executing function body (or process body). Abort allows you to jump out of one or more layers of code until exception catching code catches it.
6. Customize silent exceptions
Just like declaring a subclass of a normal exception class, just make EAbort and its subclasses ancestor classes:
TMyException = Class(EAbort);
TNextException = Class(EAbort);
etc.
Musicwind®@HangZhou.Zhejiang.China
2001-10-20
More articles
[ End of article]