Silence anomaly - Delphi helps one of the treasures

zhaozj2021-02-11  199

Silence anomaly - Delphi helps one of the treasures

Author: Musicwind®

Creation time: 2001-10-19

~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~

Update history: NO.2

Updated: 2001-10-21 02:42

Update person: MusicWind®

Update Note: Modify the format.

Update history: NO.1

Updated: 2001-10-19 21:15

Update person: MusicWind®

Update Note: Created.

~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~

Foreword: Immerse Delphi has been more than three years, but every time every time you read the help documentation of Delphi, there will be many gains, so you will not help but feel Delphi's Boda! In the embarrassment, you will be able to harvest some of them, as I am like myself, I am very literately shallow.

1. What is the abnormality of silence? (Why isn't it a silent lamb ?;-))

Silence an exception, ie Slient Exceptions refers to an exception type of a message prompt box that does not have annihiring in the default: Eabort. In Object Pascal, an exception class Eabort is a ancestral class for all silencing anomaly classes (and Eabort is inherited). Raise an Eabort will result in a stop of the module until the outermost abnormality processing module intercepts it, but does not appear a message box with a red stop flag. 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 shows two message boxes, one is "Hello1" and the other is "on exception". This shows that Eabort has indeed played a role because it jumps out "showMessage ('Hello2')" statement; at the same time, there is no "Abort IT" message box, which also confirms the inconsistency between Eabort isoms. (This is also the same during the design; and, the message box "on exception" indicates that although possible Eabort is a different number of ordinary abnormalities, this does not prevent us to capture it along the old TRY-EXCEPT statement.

2. Why do I use Eabort?

Eabort is useful in some occasions. For example, when we need to terminate an action, do not want users to perceive (do not want them to see the default exception message box). Of course, to achieve the same effect, use ordinary exceptions can also be done (such as using the try-except sentence type, put the code in the TRY segment, you need to terminate the operation, Raise is an exception, and does not write any code in the Except section), But all this is not more straightforward than using Eabort.

3. Is there something simple? - Using the ABORT process

One process ABORT defined in the SYSUTILS unit allows us to use Eabort. View Abort's implementation source code:

PROCEDURE ABORT; FUNCTION RETURNADDR: POINTER

ASM

MOV EAX, [EBP 4]

END;

Begin

Raise Eabort.create (SoperationAborted) at returnaddr;

END;

The SoperationAborted here is usually "Operation Aborted".

4. Unveil the veil - realization principle

Maybe you are like me, why is Eabort feel curious. Delphi is inside it, what is your hand to Eabort? Let us look at it.

Open a new project, click Find IN Files, enter the "eabort" keyword, then select the Search In Directorian Quantity box and set the File Mask editing box in Search Directory Option as the directory name where the Delphi source code is located (such as mine) Yes: d: / program files / borland / delphi6 / source /), don't forget to tick the include Subdirectory. Finally, click "OK" to start searching.

As a result, we found that the Delphi source code is more than 17 of Eabort. Remove the two declarations of Eabort in the sysutils unit and some comment statements, we found vast number of code similar to:

IF ExceptObject Is Eabort Then

as well as:

IF not (e is eabort) THEN

and many more.

Nothing is based on RTTI to treat Eabort - the original Eabort is so simple!

The two units worthy of attention are: Forms (Linux version qforms) and APPEVNTS, in the former code, we are more difficult to find the answer. 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 and Break and EXIT

Abort and Break are somewhat similar and different. BREAK is used to jump out a layer of loop in a circular statement. EXIT is used to jump out the currently executed function body (or process body). Abort allows you to jump out of a layer or multi-layer code section until you have an abnormal capture code to capture it.

6. Customize the abnormality of silence

Similarly to subclasses that declare an ordinary abnormality, simply use Eabort and their subclasses as ancestors:

TMYEXCEPTION = Class (eabort);

TNEXTEXCEPTION = Class (eabort);

and many more.

MusicWind®@hangzhou.zhejiang.china

2001-10-20

More articles

Culture

转载请注明原文地址:https://www.9cbs.com/read-5303.html

New Post(0)