Autor Beitrag
navarro2010
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25
Erhaltene Danke: 1



BeitragVerfasst: Mo 17.01.11 19:22 
Ich möchte ein Programm erstellen, das zwei beliebige Zahlen dividiert(x/y).
Gibt man für y null ein, soll eine Fehlermeldung kommen mit dem Hinweis 'Division durch 0'.
Ich habe das ganze ausprobiert, kann auch Zahlen dividieren, allerdings sürzt bei der Division durch 0 das Programm ab.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
  var x,y:real;
begin
  x:=StrToFloat(Edit1.Text);
  y:=StrToFloat(Edit2.Text);
try
  Panel1.Caption:=FloatToStr(x/y);
except
  ShowMessage('Division durch 0');
end
end;

end.
Teekeks
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 211
Erhaltene Danke: 23



BeitragVerfasst: Mo 17.01.11 19:28 
Starte die Exe mal außerhalb von Delphi. Du wirst sehen es geht.
Wobei ich aber vorher abfragen würde ob y=0 ist...
navarro2010 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25
Erhaltene Danke: 1



BeitragVerfasst: Mo 17.01.11 19:44 
Du hast Recht, aber das geht doch auch ohne meine Anweisung Show Message.


Zuletzt bearbeitet von navarro2010 am Mo 17.01.11 19:47, insgesamt 1-mal bearbeitet
HenryHux
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 542
Erhaltene Danke: 33

Windows 7 Premium
Delphi XE, Eclipse
BeitragVerfasst: Mo 17.01.11 19:46 
Delphi zeigt dir beim Debuggen direkt die Exceptions an, das macht es einfacher für dich =)

Lg

EDIT:
Zitat:
Du hast Recht, aber das geht doch auch ohne meine Anweisung Show Message.


Inwiefern meinst du das? Du kannst entweder nichts in den Exceptions-Block schreiben, dann gibt er dem user garkeine Meldung, wenn Division durch 0 passiert oder
du machst es so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Button1Click(Sender: TObject);
  var x,y:real;
begin
  x:=StrToFloat(Edit1.Text);
  y:=StrToFloat(Edit2.Text);
  if y<>0 then
    Panel1.Caption:=FloatToStr(x/y);
  //ggf else showmessage('Division durch 0');
end;

Für diesen Beitrag haben gedankt: navarro2010
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19313
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 17.01.11 20:09 
user profile iconHenryHux hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
procedure TForm1.Button1Click(Sender: TObject);
  var x,y:real;
begin
  x:=StrToFloat(Edit1.Text);
  y:=StrToFloat(Edit2.Text);
  if y<>0 then
Besser richtig:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
uses Math;
//...

if not IsZero(y) then
Denn sonst ist es vielleicht fast 0 und bei der Division kommt der Fehler, aber es ist eben dennoch nicht ganz exakt 0 (Rundungsfehler eben). Deshalb sollte man nie genau auf 0 prüfen.