14th Chapter-Simple database application creation and Mastapp Introduction (iii) (2)

Source: Internet
Author: User
Tags exception handling mixed

14.6.2 to implement exception protected try ... Finally statement

The above procedures are potentially dangerous, and in practice, updates to database tables may be prevented for some reason. An exception occurs when the program attempts to perform a post method to write the modified record back to disk, and because of some reason the disk is not ready. When an exception occurs, the application pauses and a dialog box pops up displaying the error message, and after the user clicks the Error message dialog box, the program continues to go somewhere, which is often not what the user expected. In our program, all parts of the form have lost contact with the ttable part before the Post method is executed. Therefore, this exception causes the data displayed in the form to be independent of the database.

Try ... in the Object Pascal. Finally statement provides a solution to the above exception problem. This statement is still used in Delphi to handle exception problems. Actually, Try ... The Finally statement is a combination of two sets of statements. The try part of the statement contains the program code that may produce an exception, and finally the last part contains one or more statements that must be executed even if an exception occurs. In this case, the Finally section contains only the Enablecontrols method call, and we rewrite and combine the preceding code into a try ... Finally statement:

With Table do

Begin

Disablecontrols; {Make other parts invalid in the process of modifying records}

Try;

A; {Point The Record pointer to the first record}

While does EOF do

Begin

< read a field value from a record to a variable >

< make the appropriate modifications >

Edit; {Place the ttable part in edit state}

< write the modified field value back to its corresponding field >

Post {Write the modified record back to the database}

Next {Modify Next record}

End

Enablecontrols;

Finally; {When an exception occurs, execute the following program}

Enablecontrols; {Restore features for other parts}

End {End Try ... Finally statement}

End

The code in the reserved word try and finally is the same as the preceding code, which is used to move the record pointer between records and to process changes to the record, which can cause exceptions, and when an exception occurs, we want to ensure that the enablecontrols is executed so that the controls in the form are restored to the Ttable parts, so we have to place the Enablecontrols statement between the finally and the closing sentence end.

Here to pay special attention, please readers do not confuse the try ... Finally statement and try ... Except statement. If you really want to take the appropriate action when an exception occurs, use the Try ... Except statement. Try ... The finally statement is only used to handle the statement that the application executes the finally part when an exception occurs, so that the program continues to execute. Try ... Except statement is implementation exception handling, Try ... Finally statement is to implement exception protection.

With these concepts, we can provide some of the program code for this example, which covers all of these things.

List of programs: Modifying records in a database

Unit Unit26;

Interface

Uses

Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms,

Dialogs, Stdctrls, Grids, Dbgrids, Extctrls, DB, Dbtables, Buttons;

Type

TForm1 = Class (Tform)

Datasource1:tdatasource;

customertable:ttable;

Panel1:tpanel;

Dbgrid1:tdbgrid;

Panel2:tpanel;

Uppercasefirstaddbtn:tbutton;

Uppercasesecondaddbtn:tbutton;

Mixedcasefirstaddbtn:tbutton;

Mixedcasesecondaddbtn:tbutton;

BITBTN1:TBITBTN;

Procedure Forcecase (targetfield:string; Toupper:boolean);

Procedure Uppercasefirstaddbtnclick (Sender:tobject);

Procedure Mixedcasefirstaddbtnclick (Sender:tobject);

Procedure Uppercasesecondaddbtnclick (Sender:tobject);

Procedure Mixedcasesecondaddbtnclick (Sender:tobject);

Procedure Formcreate (Sender:tobject);

Private

{Private declarations}

Public

{Public declarations}

End

Var

Form1:tform1;

Implementation

Const

Upper=true;

Mixed=false;

{$R *. DFM}

Function Isupper (Ch:char): Boolean;

Begin

If (ch>= ' A ') and (ch<= ' Z ') then

Isupper:=true

Else

Isupper:=false;

End

Procedure Tform1.forcecase (targetfield:string; Toupper:boolean);

Var

workbuffer:string;

I:integer;

Begin

With Customertable do

Begin

Disablecontrols;

TRY

A; {Move the record pointer to the first record]

While does EOF do

Begin

Workbuffer:=fieldbyname (Targetfield). asstring;

If ToUpper Then

For I:=1 to Length (Workbuffer) do

Workbuffer[i]:=upcase (Workbuffer[i])

Else

Begin

For I:=1 to Length (Workbuffer) do

If Isupper (Workbuffer[i]) then

WORKBUFFER[I]:=CHR (Ord (Workbuffer[i]) +32);

Workbuffer[1]:=upcase (Workbuffer[1])

End

Edit;

Fieldbyname (Targetfield). Asstring:=workbuffer;

Post

Next;

End

Finally

Enablecontrols;

End

End

End

Procedure Tform1.uppercasefirstaddbtnclick (Sender:tobject);

Begin

Forcecase (' Addr1 ', Upper);

End

Procedure Tform1.mixedcasefirstaddbtnclick (Sender:tobject);

Begin

Forcecase (' Addr1 ', Mixed);

End

Procedure Tform1.uppercasesecondaddbtnclick (Sender:tobject);

Begin

Forcecase (' ADDR2 ', Upper);

End

Procedure Tform1.mixedcasesecondaddbtnclick (Sender:tobject);

Begin

Forcecase (' ADDR2 ', Mixed);

End

Procedure Tform1.formcreate (Sender:tobject);

Begin

Customertable.open;

End

End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.