The so-called exception is that during the program operation, the suspension procedure caused by the problem itself or the improper operation caused by improper operation of the user. People who write process in Delphi will definitely be unfamiliar. Abnormal source is multifaceted, reference null pointers, assignment offshore, zero-proof, etc., can trigger an exception. If the abnormal event is not properly processed, it is likely to cause crash of the entire program. Fortunately, Delphi can automatically process almost all abnormalities that may occur.
I. Simple example of Delphi automatic processing exception
1. In the Delphi (3.0) integration environment, select the Tools | Environment Options menu, there is a Break on Exception option on the Preferences page in the Environment Options window that appears. This option is selected in the default, so running the program in the Delphi integrated environment, the program will automatically break back and return to the Delphi debug state, the cursor stays on the code that appears in an exception to modify the programmer . Click this option to make it not selected, then determine.
2. Create a new project, except (still useful), the corresponding unit is excpUnit.pas; put a Tmaskedit component in Form1, set its EditMask property to Date, then put a TButton component Double-click, write its onclick event as follows:
Procedure TFORM1.BITBTN1CLICK (Sender: TOBJECT);
VAR K: Integer;
Begin
K: = 0;
K: = 9 DIV K; {here will result in an abnormality from 0 to 0}
END;
3. The deployment and run the program, write one or two numbers in Maskedit1, enter, and will generate (also known as evoke) an exception, pop up the following window:
This is due to incomplete data in the Maskedit box, and Delphi automatically processes, and then proceedes to continue execution after "OK". Click the Button1 button, there will be another similar window because there is an exception that is zero.
Second, Delphi's prompt to the abnormal situation is English, which is not used to Chinese people, we can intercept these abnormalities and implement the Chinese tips. The specific practices 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 ('divisor cannot be zero');
END;
END;
2. The save and run the program, click the prompt box that appears after button1 will be replaced by Chinese "divisor cannot be zero". Using the try ... except ... End statement is a common method of processing an exception, in the reserved word try, if an exception is performed, the statement between the reserved word Except and End is executed, otherwise Execute the statement behind the end, which replaces the default processing of exceptions in Delphi. The other statement similar to it is TRY ... FINALLY ... END, with TRY ... EXCEPT ... END, no matter how the TRY is Will not produce an exception, Finally's statement will be executed.
However, for such exceptions generated by the Tmaskedit box, use the try statement to be unable to force because we can't find the place where the TRY statement is written. We can only use another way to solve it. Third, every project in Delphi has a Tapplication object, which is an invisible object. We can implement control of special exceptions by modifying its Onexception events, and concrete practices are as follows:
1 Declare a process MyException in the form of Form1, which has the same parameters as the TAPPLICATION's OneXception event:
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 EdbeditEnror Then ShowMessage ('Input does not meet the rules')
Else
Application.showException (e); {Call the default exception handler}
END;
3. Assign the oneXception event for Tapplication in the oncreate event of Form1:
Procedure TFORM1.FormCreate (Sender: TOBJECT);
Begin
Application.onexception: = MyException;
END;
4. The save and run the program, enter one or two numbers in the Maskedit box, enter, and a Chinese prompt box will replace the original English prompt box.
Fourth, here remind everyone to pay attention to three points:
1.Delphi summarizes all anomaly into a class, an exception, each particular exception is considered a special case, in the C: / Program files / borland / delphi 3 / source / RTL / SYS / directory There is a definition of the Exception class in sysutils.pas.
The 2.Tapplication object provides a process handleException to handle an exception event that is evoked and unprocessed in the program. When we assign a value for the TAPPLICATION, HandleException calls the new process to replace the default error message display. We can freely arrange the content displayed in your own process to achieve the effect of Chinese.
3. We get the specified exception by IF judgment statement, and use a simple way to get the name of the specified exception. Go back to the example of the article, here we select the Break on Exception option, run the program again, and the incoming number makes MasKedit1 abnormally, then the following window appears:
The second row in the window is the name of the exception, the code in this article:
IF e is edbeditEnror Then ShowMessage ('input does not meet the regulations ")
It is written according to this name. When you don't know the exact name of the abnormality, you can get information here. When using this method to handle an abnormal event, be careful, you should replace Delphi's default anomalous processing, if the processing is improperly crashed.
The above procedures run in Windows 95 Delphi 3.0.