12.1.3 part Exception class
12.1.3.1 Generic Part Exception class
Common parts exception classes are commonly used in three: Einvalidoperation, Ecomponenterror, Eoutofresource. Where einvalidoperation and eoutofresource are defined in the controls unit, Ecomponenterror are defined in the classes unit.
1. Illegal operation Abnormal Einvalidoperation
Einvalidoperation causes may be:
The application attempted to perform some operations that require a Windows handle on a part of the parent property that is nil
An attempt was made to drag and drop an operation on a window
The operation violates the built-in interrelationships between part attributes, etc.
For example, parts such as ScrollBar, gauge require the Max property to be greater than or equal to the min attribute, so the following statement:
Scrollbar1.max: = scrollbar1.min-1;
A Einvalidoperation exception is thrown.
2. Abnormal parts Ecomponenterror
This exception may be raised for the following reasons:
Attempting to log on to a part outside of the register process (often used in custom part development)
The application changes the name of a part in the run and makes the part duplicate with another part
The name of a part is changed to an illegal identifier of object Pascal
Dynamically generate a part with the same name as another part that already exists
3. Abnormal resource depletion Eoutofresource
This exception is thrown when an application attempts to create a Windows handle and Windows does not have an extra handle assignment.
12.1.3.2 Special Parts Exception class
Many parts define the corresponding part exception class. However, not all errors related to the part will raise the corresponding exception class. In many cases they will throw a Run-time exception or an object exception.
Several typical part exception classes are listed below.
1.EMenuError
Illegal menu action, such as trying to delete a nonexistent menu item. This exception class is defined in the Menus Library unit.
2.EInvalidGridOpertion
Illegal grid operations, such as trying to reference a nonexistent grid unit. This exception class is defined in the Grids Library unit.
3.EDDEError
A DDE exception. For example, an application cannot find a specific server or session, or a join is aborted unexpectedly. This exception class is defined in the Ddeman Library unit.
4.edatabaseerror,ereporterror
Database exceptions (Edatabaseerror) and report exceptions (Ereporterror) are raised when there are errors in database and report operations. Please refer to the second part of this book for questions about the database.
Summary of 12.1.4
In this section, the article focuses on the exception-type system provided by Delphi. We strive to give readers a clear and comprehensive impression that allows them to actually use them in their own program development. We also provide some simple descriptive examples for easy understanding. Although readers may encounter many problems in the specific use, it is a sign that the level of programming is on the new stage, but it is realized that the exception class should be used to enhance the robustness of the program.
12.2 Exception Protection
Ensuring that resources are recycled is a key to the robustness of the program. However, when an exception occurs by default, the program automatically exits the current module at the error point, so a special mechanism is needed to ensure that the statement that frees the resource even if the exception occurs is still executed. and Delphi's exception handling is providing this mechanism.
12.2.1 resources that need to be protected
Generally, the resources to be protected include:
File
Memory
Windows resources
Object
For example, the following section of the program will result in the loss of 1 K memory resources.
Var
Apointer:pointer;
Aint, Adiv:integer;
Begin
Adiv: = 0;
Getmem (Apointer, 1024);
Aint: = ten div adiv;
Freemem (Apointer, 1024);
End
Because the program exits from the exception point, Freemem never has an opportunity to execute.