Home > Back-end >  Except function returns false, the program execution.
Except function returns false, the program execution.

Time:10-30

The function TForm1. Getid (id: string) : Boolean;
The begin
If id='0' then Result:=True
The else
Result:=False;
end;

Procedure TForm1. BT1Click (Sender: TObject);
The begin
Try
Getid (edt1. Text);
ShowMessage (' 0 ');
Except,
ShowMessage (' 1 ');
end;
end;

Question: if getid () function returns false, equivalent to try to catch errors, how to make the program execution except under the program, the output is 1?

CodePudding user response:

Procedure TForm1. Button1Click (Sender: TObject);
The begin
Try
If getid (edit1. Text) then
ShowMessage (' 0 ')
The else
Raise the Exception. The Create (');
Except,
ShowMessage (' 1 ');
end;
end;

CodePudding user response:

Upstairs positive solution, throw an exception, will jump to the Except,

CodePudding user response:

Who told you that function returns false is the exception? ! -_ -!

CodePudding user response:

You this code does not perform returns false ShowMessage (' 1 ');
Try the except there is exception to execute the code inside the

For example:
Try
X:=strtoint (edit1. Text);
Except,
Showmessage (' 1 ');
end;

If edit1 input is not number, abnormal, will be executed showmessage (' 1 ');

CodePudding user response:

Just judge true/false, you don't have to use the except the

If getid (edt1. Text) then
ShowMessage (' 0 ')
The else
ShowMessage (' 1 ');
  • Related