Try ... Except usage

xiaoxiao2021-03-06  77

[Inland]

http://delphi.ktop.com.tw/topic.asp?topic_id=53541

Shinhrn Tryadoconnection1.begintrans;

// Data processing

Adoconnection1.committrans; Exceptadoconnection1.rollbackTrans; showMessage ('archive failed!');

If an error occurs, RollBackTrans can be executed to cancel all anime action in all data processing, such as (write, delete, etc.) ....

The BigDogchina try statement is an auxiliary process to control the execution of the program, often uses Abort, EXIT, etc. Let's take a look at this:

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

Try

if True Then

Abort;

Except // Abort excitation exception is captured, so the exception code in ExcePt ... End is executed CODE

SHOWMESSAGE ('Abort');

END;

ShowMessage ('Processing!'); // Execution, because the exception has been processed, if you use try ... finally, the sentence will not be processed

END;

Note: If Abort is included in a Try ... Except, only Tr ... Except is exited, then execute the ExcePt ... End block, and finally the following statement is continued. If included in the Try ... finally block, execute the Finally ... End block and then exit the process

Let's take a look at it and exit looks.

Procedure tfrom1.button2click (sender: TOBJECT);

Begin

Try

if True Then

EXIT;

Finally

ShowMessage ('Abort'); //

END;

ShowMessage ('handling "); // does not execute

END;

Here is the use of try ... finally, you can also match it.

HLT and

RUNERROR

And Try often uses Continu and Break, let's take a look:

Procedure TFORM1.BUTTON3CLICK (Sender: tojbect);

VAR

I: integer;

Begin

For i: = 0 to 2 do

Begin

Try

IF i <2 THEN

Continue; // Please note that continchee is here

ShowMessage (INTSTR (i));

Finally

ShowMessage ('This is Finally!');

END;

END;

END;

Continue If located in the Try ... finally block, you will first perform Finally ... END and then enter the next loop. The above example execution result is: first display two times' this is finaly! '(Corresponding to i = 0 and 1), then display once I (corresponding i = 2), and finally display a time I (or corresponding i = 2).

But Break's usage is not quite the same, if it is in the Try ... finally block, Finally ... End block will also be executed, see below:

Procedure TFORM1.BUTTON4CLICK (Sender: TOBJECT);

VAR

I: integer;

Begin

For i: = 0 to 2 do

Begin

Try

IF I> 0 THEN

Break;

ShowMessage (INTSTR (i)); Finally

ShowMessage ('This is in Finally');

END;

END;

END;

In summary, you can see Try ... End ... End and Try ... Finally ... End and Abort, Exit, Halt, RuNerror, Continue, Break usage! Thank you

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

New Post(0)