The so-called exception refers to a temporary error caused by problems in the program itself or improper operation by the user during the running of the program.
Stop program execution and produce erroneous results. Anyone who has written programs in Delphi will definitely be familiar with exceptions. source of exception
There are many aspects. Reference to a null pointer, assignment out of bounds, division by zero, etc. can all cause an exception. If appropriate measures are not taken for abnormal events,
Improper handling may cause the entire program to crash. Fortunately, Delphi can automatically handle almost all exceptions that may occur.
deal with.
1. A simple example of Delphi automatically handling exceptions
1. In the Delphi (3.0) integrated environment, select the Tools | Environment Options menu, and in the Environment that appears
There is a Break on exception option on the PReferences page in the Options window (in the lower left). This option is by default
The status is selected, so that if an exception occurs when running the program in the Delphi integrated environment, the program will automatically be interrupted and return
In Delphi debugging state, the cursor stays on the code where the exception occurs so that programmers can modify it. Now click on this option to make it unavailable
Check it and OK.
2. Create a new project and save it as Excep.dpr (it will be useful later), and save the corresponding unit as Excpunit.pas; in Form1
Put a TmaskEdit component in it, set its EditMask property to Date, then put a Tbutton component, double-click it,
Write its OnClick event as follows:
procedure TForm1.BitBtn1Click(Sender: TObject);
var k:integer;
begin
k:=0;
k:=9 div k;{A division by 0 exception will be generated here}
end;
3. Save and run the program. Just write one or two numbers in MaskEdit1 and press Enter. This will generate (also called awakening)
An exception pops up the following window:
This is an exception due to incomplete data entered in the MaskEdit box. Delphi automatically handles it. Click
After "OK", the program continues execution. Click Button1, another similar window will appear, because there is a
Division by zero exception.
2. Delphi’s prompts for abnormal situations are in English, which is not used by Chinese people. We can intercept these exceptions and implement them using
Chinese tips. The specific steps are as follows:
1. Modify the OnClick event of Button1 as follows:
procedure TForm1.BitBtn1Click(Sender: TObject);
var k:integer;
begin
k:=0;
try
k:=9 div k;
except
showmessage('The divisor cannot be zero');
end;
end;
2. Save and run the program. The prompt box that appears after clicking Button1 will be replaced by the Chinese message "The divisor cannot be zero." use
The try...except...end statement is a common method of handling exceptions. The statement after the reserved word try is executed if
If an exception occurs, the statement between the reserved words except and end will be executed. Otherwise, the statement after end will be executed, thus replacing Delphi's
Default handling of exceptions. Another similar statement is try...finally...end, which is the same as try...except..
....The difference with end is that regardless of whether the statement after try will generate an exception, the statement after finally will definitely be executed.
However, for exceptions like those generated by the TmaskEdit box, using the try statement is powerless because we cannot find
A place to write try statements. We can only solve it in another way.
3. Every project in Delphi has a Tapplication object, which is an invisible object. we can
By modifying its OnException event, you can control special exceptions. The specific methods are as follows:
1. Declare a process Myexception in the class of Form1. This process is related to the OnException event of Tapplication.
Same parameters:
public
{Public declarations}
procedure MyException(Sender:TObject;E:Exception);
2. Write the process code:
procedure TForm1.MyException(Sender:TObject;E:Exception);
begin
if E is EDBEditError then showmessage(‘The input does not comply with the rules’)
else
Application.ShowException(E); {Calling default exception handling}
end;
3. Assign a value to Tapplication’s OnException event in the OnCreate event of Form1:
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnException :=MyException;
end;
4. Save and run the program, enter one or two numbers in the MaskEdit box, press Enter, and a Chinese prompt box will appear.
Replace the original English prompt box.
4. Here we would like to remind everyone to pay attention to three points:
1.Delphi summarizes all exceptions into one class, that is, exception class (Exception). Each specific exception is regarded as
A special case, in sysutils.pas in the c:program files orlandDelphi 3source tlsys directory
There is a definition of Exception class.
2. The Tapplication object provides a process HandleException to handle unhandled errors that are evoked in the program.
Exception events, when we assign a value to Tapplication's OnException event, HandleException will call the new
Procedure to replace the default error message display. We are free to arrange the displayed content in our own process to achieve Han
transformation effect.
3. We use the IF judgment statement to obtain the specified exception. We can use a simple method to get the name of the specified exception.
Returning to the example at the beginning of the article, here we select the Break on exception option, run the program again, and enter
Improper numbers cause MaskEdit1 to generate an exception, and the following window will appear:
The EDBEditError in the second row of the window is the name of the exception. The code in the program in this article:
if E is EDBEditError then showmessage(‘The input does not meet the requirements’)
It was written based on this name. This is where you can get the information when you don't know the exact name of the exception. Using this
Be careful when handling exception events because you have replaced Delphi's default exception handling.
This may cause the system to crash.
The above program runs successfully under Windows 95 Delphi 3.0.