AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

exception handling im konstruktor

Offene Frage von "Konse"
Ein Thema von Konse · begonnen am 21. Feb 2007 · letzter Beitrag vom 21. Feb 2007
 
Konse

Registriert seit: 20. Feb 2007
Ort: Bayern
27 Beiträge
 
Delphi 7 Professional
 
#1

exception handling im konstruktor

  Alt 21. Feb 2007, 20:41
Hi,
wie würdet ihr am besten eine Exception im construktor handlen?

Delphi-Quellcode:
class TCMyClass
private
mInt:Integer;
public
constructor create(const myInt:integer);
end;

constructor TCMyClass.create(const myInt:integer);
begin
if not inRange(myInt, 5,19) then raise Exception.create('value is not in Range..');
mInt:=myInt;
end;
Bis jetzt sehe ich nur folgende möglichkeit:

Delphi-Quellcode:
function doSomething(const toCheck:integer):boolean;
var
lala:TCMyClass;// toCheck ist z.b. integer der nur von 5-19 gehen soll ansonsten exception
begin
  result:=false;

   try
      lala:=TCMyClass.Create(toCheck);
   except
      lala:=nil;
      exit;
   end;

   try
      lala.doWork();
   finally
     FreeAndNil(lala);
   end;
end;
Falls es elegantere Möglichkeiten gibt Preconditions im Konstruktor abzudecken würde
ich mich freuen welche zu sehen

gruß konse
Warum ich Klingonische Programmierer so liebe:
- A true Klingon programmer does not comment his code
- You cannot truly appreciate Dilbert unless you´ve read in the original klingon
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 07:13 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz