Autor Beitrag
HCN
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 22

Xp Prof Sp2, Vista ultimate Sp1
C#, C++, VS 2008 Prof, MeVisLab
BeitragVerfasst: So 08.06.08 19:07 
Hallo,

ich hoffe mal ich poste das hier im richtigen Unterforum :-)

Ich habe ein Programm geschrieben welches mir ein Windows Form erstellt und eine Baumstruktur rein zeichnet.
Was in der Baumstruktur stehen soll steht in einem File welches ich am Anfang laden lasse.

Es funktioniert auch alles ohne Probleme, nur wenn das File nicht da sein sollte bekomme ich das Exeption Handling nicht vernünftig geregelt.

Vielleicht könnt ihr mir helfen. Hier mal der Codeausschnitt:

Eigentlich dachte ich, es ist kein Problem, wenn er das File nicht findet, springt er zum catch Handler, bringt die Fehlermeldung und beendet das Programm, fertig.
Aber das macht er nicht. Er springt zwar zum catch Handler und die MessageBox bringt er auch, aber er geht trotzdem weiter zu meiner makeTree Methode und meckert dann dort rum weil mein RichTextBoxEx Array schachteln leer ist....

Warum beendet er das Programm nicht bei Application.Exit(); ?

ausblenden C#-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:
public partial class Form1 : Form
    {
        private string[] zeilen;
        private RichTextBoxEx[] schachteln;
        static private int linienHoehe = 4;
        private int yPosition = 0 + linienHoehe/2;

        public Form1()
        {
            InitializeComponent();

            try
            {
                zeilen = File.ReadAllLines("Test.tree", Encoding.Default);
                schachteln = new RichTextBoxEx[zeilen.Length];
            }
            catch(FileNotFoundException)
            {
                MessageBox.Show("*.tree Datei wurde nicht gefunden!""Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }

            makeTree();    
        } // Ende method Form1


Vielen Dank HCN


Moderiert von user profile iconChristian S.: Topic aus C# - Die Sprache verschoben am So 08.06.2008 um 19:10
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: So 08.06.08 19:29 
Hi und :welcome:!

Exceptions sollten eigentlich für Dinge sein, die man nicht vorher sieht. In diesem Fall würde ich es einfach mal mit File.Exists versuchen ;-)

Zu der Frage, weshalb die Anwendung nicht beendet wird, erstmal ein bisschen was aus der Doku zu Application.Exit:
Doku hat folgendes geschrieben:
The Exit method stops all running message loops on all threads and closes all windows of the application. This method does not necessarily force the application to exit.


In diesem Fall sieht es so aus, dass Du im Konstruktor von Form1 Application.Exit aufrufst. Aber schau Dir mal an, wie das in der program.cs aussieht:
ausblenden C#-Quelltext
1:
Application.Run(new Form1());					

Erst kommt der Konstruktor, dann erst wird die Application wirklich gestartet und das Fenster geöffnet.

Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
HCN Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 22

Xp Prof Sp2, Vista ultimate Sp1
C#, C++, VS 2008 Prof, MeVisLab
BeitragVerfasst: So 08.06.08 20:37 
Vielen Dank!

Jetzt geht es!