![]() |
AW: Try except - Functions - wie tief
Nein, es sollte eher so aussehen
Delphi-Quellcode:
Du kannst jetzt 3 mal auf den Button klicken und es wird beim dritten Klick eine Exception angezeigt!
unit Form.Main;
interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm1 = class( TForm ) Button1: TButton; procedure Button1Click( Sender: TObject ); private FDateiListe: TStringList; public procedure AfterConstruction; override; procedure BeforeDestruction; override; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.AfterConstruction; begin inherited; FDateiListe := TStringList.Create; // Ausgangssituation im Ordner tmp // TestFoo.txt // TestBar.txt // TestBarMove.txt // TestFooBar.txt // TestFooBarMove.txt // TestFooBarMove_1.txt // Am Ende finden wir dort // TestFooMove.txt <- alte TestFoo.txt // TestBarMove.txt // TestBarMove_1.txt <- alte TestBar.txt // TestFooBar.txt <- konnte nicht kopiert werden // TestFooBarMove.txt // TestFooBarMove_1.txt FDateiListe.Values['.\tmp\TestFoo.txt'] := '.\tmp\TestFooMove.txt'; FDateiListe.Values['.\tmp\TestBar.txt'] := '.\tmp\TestBarMove.txt'; FDateiListe.Values['.\tmp\TestFooBar.txt'] := '.\tmp\TestFooBarMove.txt'; end; procedure TForm1.BeforeDestruction; begin FDateiListe.Free; inherited; end; procedure BewegeDatei( const ASource, ADestination: string ); begin if not MoveFile( PChar( ASource ), PChar( ADestination ) ) then RaiseLastOSError; end; procedure DateiUmbenennen( const ASource, ADestination: string ); var LNewDestination: string; begin LNewDestination := ChangeFileExt( ADestination, '_1.txt' ); BewegeDatei( ASource, LNewDestination ); end; procedure TForm1.Button1Click( Sender: TObject ); var LSource, LDestination, LNewDestination: string; begin LSource := FDateiListe.Names[0]; LDestination := FDateiListe.ValueFromIndex[0]; try BewegeDatei( LSource, LDestination ); except on E: EOSError do case E.ErrorCode of ERROR_ALREADY_EXISTS: // Hier können wir etwas machen DateiUmbenennen( LSource, LDestination ); else raise; end; end; FDateiListe.Delete( 0 ); end; end. Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:13 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz