Autor Beitrag
counterto
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 123

Win XP
Delphi 2005 Prof., Visual Basic 2005, PHP, My Sql, Visual Basic 6
BeitragVerfasst: Sa 11.02.06 12:00 
Hallo,

ich bin in Delphi 2005 eingestiegen und habe jetzt einen kleinen "Rechner" programmiert! Doch wenn cih x:0 teile, erhalte ich einen Fehler. Kann man das mit einer If-Gleichung wegmachen, so dass "if zahl2 = 0 then ..." ? Ich brauche Hilfe! Danke!

mfg

countero


Moderiert von user profile iconChristian S.: Topic aus Sonstiges (Delphi) verschoben am Sa 11.02.2006 um 12:00
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 11.02.06 12:05 
Hallo,

ausblenden Delphi-Quelltext
1:
2:
3:
4:
if zahl2=0 then
showmessage('Division durch Null1') else
//hier machst du dann weiter mit der Division
//eventuell musst du noch ein 'Begin' hier einfügen

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Sa 11.02.06 12:29 
laut gausi machst es besser nicht so! :lol:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.Button1Click(Sender: TObject);
begin
 try
  label1.Caption := FloatToStr(StrToFloat(edit1.Text) / StrToFloat(edit2.Text));
 except
  on EZeroDivide do
  begin
    showmessage('Achtung: Division duch Null!');
   end;
  end;
 end;


Zuletzt bearbeitet von Marc. am Sa 11.02.06 12:40, insgesamt 2-mal bearbeitet
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Sa 11.02.06 12:33 
Wieso soll das besser sein?
  • Du mischst Anzeige und Verarbeitung. Besser wäre eine strikte Trennung von Zahlen ermitteln, Werte verarbeiten und Werte ausgeben
  • deine "Hauptzeile" kann an drei Stellen eine Exception auslösen. Zweimal bei StrToFloat, einmal bei /. Du fängst aber nur eine gesondert ab.
  • Warum ist es besser, eine Exception auszulösen, anstatt sie vorher abzufangen?

_________________
We are, we were and will not be.
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Sa 11.02.06 12:39 
• es war nur ein beispiel...
• ich fand es einfach eleganter... :oops: :lol:
Grenzgaenger
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 11.02.06 12:56 
das exception handling braucht mehr resourcen. daher ist's besser die fehler erst mal zu vermeiden anstatt fehler zu verursachen und sie dann zu behandeln. mein favorit ist daher

ausblenden Delphi-Quelltext
1:
2:
3:
4:
if a = 0 then
 fehler
else
 dividiere;


Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt