Korrektes Fehler abfangen

  • VB.NET
  • .NET (FX) 3.0–3.5

Es gibt 9 Antworten in diesem Thema. Der letzte Beitrag () ist von Orion.

    Korrektes Fehler abfangen

    Guten Tag Zusammen,

    Ich habe hier ein Snippet aus meinem Programm welches eine Datei einliest welche zuvor angeklickt wurde und dann den Inhalt ins Programm lädt und in die gegebenen Felder abfüllt. Da habe ich noch nicht alles gemacht der Übersichtshalber aber bevor ich dies vervollständige möchte ich euch fragen wo und wie ich korrekt Fehler abfangen kann. Bei einem Testlauf habe ich immer Fehler mit dem Datumsformat darum würde ich dieses gerne abfangen und überspringen falls es zu einem Fehler kommt. Try-Catch ging nicht, weil die Variable nacher weiterverwendet wird oder bereits vorher genutzt wird...

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.IO
    2. Imports System.Linq
    3. Imports System.Environment
    4. Public Class FormAutoComplete
    5. 'Dim Dateiliste() As String = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Dokumente\Indexierung", "*.*", System.IO.SearchOption.TopDirectoryOnly)
    6. Public scrWidth As Integer = Screen.PrimaryScreen.Bounds.Width
    7. Public scrHeight As Integer = Screen.PrimaryScreen.Bounds.Height
    8. Public poswid As Integer = scrWidth \ 2
    9. Public poswidi As Integer = poswid - 467
    10. Public poshei As Integer = scrHeight \ 2
    11. Public _isbestkeyword As String = ""
    12. Public _iskeyword As Boolean
    13. Public woerterbuchS As New List(Of String)
    14. Public appData As String = GetFolderPath(SpecialFolder.ApplicationData)
    15. Public path As String = appData & "\Dokumente"
    16. Public Sub myForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    17. Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    18. Me.ListBox1.Dock = DockStyle.Fill
    19. End Sub
    20. Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    21. _isbestkeyword = Me.ListBox1.Items(Me.ListBox1.SelectedIndex)
    22. End Sub
    23. Protected Overrides ReadOnly Property ShowWithoutActivation() As Boolean
    24. Get
    25. Return True
    26. End Get
    27. End Property
    28. Public i As Integer = 0
    29. Public Sub RefreshList(ByVal key As String)
    30. Dim Files = New System.IO.DirectoryInfo(path + "\Indexierung").GetFiles("*.*", IO.SearchOption.TopDirectoryOnly)
    31. While i <> 1
    32. woerterbuchS.AddRange(Files.Select(Function(f) f.Name).ToArray())
    33. i = i + 1
    34. End While
    35. Dim LA() As String
    36. LA = woerterbuchS.ToArray
    37. Dim all = LA.Length
    38. Me.ListBox1.Items.Clear()
    39. find_key_arg = key & "*"
    40. Dim t() As String
    41. t = Array.FindAll(LA, AddressOf find_key)
    42. If t.Count > 0 Then
    43. If key = "" Or t(t.Length - 1) = key Then
    44. _iskeyword = False
    45. Me.ListBox1.Items.Clear()
    46. Me.Hide()
    47. Else
    48. _iskeyword = True
    49. Me.ListBox1.Items.Clear()
    50. Me.ListBox1.Items.AddRange(t)
    51. Me.ListBox1.SelectedIndex = 0
    52. Me.Show()
    53. End If
    54. Else
    55. _iskeyword = False
    56. Me.Hide()
    57. End If
    58. End Sub
    59. Private Sub Main_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
    60. Me.Location = New Point(Main.Location.X - Me.Width, Main.Location.Y)
    61. End Sub
    62. Private Sub listBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
    63. Try
    64. Dim ValCon As Date
    65. Dim ValCon2 As Date
    66. Dim Valcon3 As Date
    67. Dim reader = My.Computer.FileSystem.ReadAllText(path & "\Indexierung\" + IsBestKeyWord)
    68. Dim parts = reader.Split(";"c)
    69. Dim part12 = parts(40).ToString.Split(" "c)
    70. Dim tripple2 = part12(0).Split("."c)
    71. Main.RichTextBox1.Text = parts(0)
    72. Main.RichTextBox2.Text = parts(1)
    73. Main.RichTextBox3.Text = parts(2)
    74. Main.RichTextBox4.Text = parts(3)
    75. Dim part1 = parts(4).ToString.Split(" "c)
    76. Dim tripple = part1(0).Split("."c)
    77. Dim EndDate As String = tripple(0) + "/" + tripple(1) + "/" + tripple(2)
    78. ValCon = Date.ParseExact(EndDate, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None)
    79. Main.DateTimePicker2.Value = ValCon
    80. Main.RichTextBox5.Text = parts(5)
    81. Main.RichTextBox6.Text = parts(6)
    82. Main.RichTextBox7.Text = parts(7)
    83. Main.RichTextBox8.Text = parts(8)
    84. Main.TextBox9.Text = parts(9)
    85. Main.TextBox1.Text = parts(10)
    86. Main.ComboBox12.SelectedIndex = -1
    87. Main.ComboBox12.SelectedText = parts(14).ToString
    88. Main.TextBox10.Text = parts(15)
    89. Main.TextBox11.Text = parts(16)
    90. Main.RichTextBox9.Text = parts(17)
    91. If parts(18).ToString = "True" Then
    92. Main.CheckBox1.Checked = True
    93. Else
    94. End If
    95. If parts(19).ToString = "Frei" Then
    96. Main.RadioButton1.Select()
    97. Else
    98. Main.RadioButton2.Select()
    99. End If
    100. Main.RichTextBox13.Text = parts(20)
    101. Main.RichTextBox11.Text = parts(21)
    102. Main.TextBox4.Text = parts(22)
    103. Main.TextBox8.Text = parts(23)
    104. Main.TextBox2.Text = parts(24)
    105. Main.TextBox5.Text = parts(25)
    106. Main.TextBox6.Text = parts(26)
    107. Main.TextBox7.Text = parts(27)
    108. Main.TextBox3.Text = parts(28)
    109. Main.ComboBox21.SelectedIndex = -1
    110. Main.ComboBox21.SelectedText = parts(29).ToString
    111. Main.ComboBox16.SelectedIndex = -1
    112. Main.ComboBox16.SelectedText = parts(30).ToString
    113. Main.ComboBox20.SelectedIndex = -1
    114. Main.ComboBox20.SelectedText = parts(31).ToString
    115. Main.ComboBox7.SelectedIndex = -1
    116. Main.ComboBox7.SelectedText = parts(32).ToString
    117. Main.ComboBox9.SelectedIndex = -1
    118. Main.ComboBox9.SelectedText = parts(33).ToString
    119. Main.ComboBox15.SelectedIndex = -1
    120. Main.ComboBox15.SelectedText = parts(34).ToString
    121. Main.ComboBox22.SelectedIndex = -1
    122. Main.ComboBox22.SelectedText = parts(35).ToString
    123. Main.RichTextBox12.Text = parts(36)
    124. Main.RichTextBox10.Text = parts(37)
    125. Main.ComboBox4.SelectedIndex = -1
    126. Main.ComboBox4.SelectedText = parts(38).ToString
    127. Main.TextBox12.Text = parts(39)
    128. Dim EndDate2 As String = tripple2(0) + "/" + tripple2(1) + "/" + tripple2(2)
    129. ValCon2 = Date.ParseExact(EndDate2, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None)
    130. Main.DateTimePicker1.Value = ValCon2
    131. Main.RichTextBox14.Text = parts(41)
    132. Main.RichTextBox15.Text = parts(42)
    133. Main.RichTextBox16.Text = parts(43)
    134. Dim part13 = parts(44).ToString.Split(" "c)
    135. Dim tripple3 = part13(0).Split("."c)
    136. Dim EndDate3 As String = tripple3(0) + "/" + tripple3(1) + "/" + tripple3(2)
    137. Valcon3 = Date.ParseExact(EndDate3, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None)
    138. Main.DateTimePicker3.Value = Valcon3
    139. Main.ComboBox2.SelectedIndex = -1
    140. Main.ComboBox2.SelectedText = parts(45).ToString
    141. Catch Ex As Exception
    142. MsgBox("Fehler beim abfüllen der Daten")
    143. MsgBox(Ex.ToString)
    144. End Try
    145. End Sub
    146. Public ReadOnly Property IsKeyWord() As Boolean
    147. Get
    148. Return _iskeyword
    149. End Get
    150. End Property
    151. Public ReadOnly Property IsBestKeyWord() As String
    152. Get
    153. If _iskeyword = True Then
    154. Return _isbestkeyword
    155. Else
    156. Return ""
    157. End If
    158. End Get
    159. End Property
    160. Private find_key_arg As String
    161. Private Function find_key(ByVal s As String) As Boolean
    162. If s Like find_key_arg Then
    163. Return True
    164. Else
    165. Return False
    166. End If
    167. End Function
    168. Dim ValCon As Date
    169. Dim ValCon2 As Date
    170. Dim Valcon3 As Date
    171. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    172. Try
    173. Dim ValCon As Date
    174. Dim ValCon2 As Date
    175. Dim Valcon3 As Date
    176. Dim reader = My.Computer.FileSystem.ReadAllText(path & "\Indexierung\" + IsBestKeyWord)
    177. Dim parts = reader.Split(";"c)
    178. Main.RichTextBox1.Text = parts(0)
    179. Main.RichTextBox2.Text = parts(1)
    180. Main.RichTextBox3.Text = parts(2)
    181. Main.RichTextBox4.Text = parts(3)
    182. Dim part1 = parts(4).ToString.Split(" "c)
    183. Dim tripple = part1(0).Split("."c)
    184. Dim EndDate As String = tripple(0) + "/" + tripple(1) + "/" + tripple(2)
    185. ValCon = Date.ParseExact(EndDate, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None)
    186. Main.DateTimePicker2.Value = ValCon
    187. Main.RichTextBox5.Text = parts(5)
    188. Main.RichTextBox6.Text = parts(6)
    189. Main.RichTextBox7.Text = parts(7)
    190. Main.RichTextBox8.Text = parts(8)
    191. Main.TextBox9.Text = parts(9)
    192. Main.TextBox1.Text = parts(10)
    193. Main.ComboBox12.SelectedText = parts(14).ToString
    194. Main.TextBox10.Text = parts(15)
    195. Main.TextBox11.Text = parts(16)
    196. Main.RichTextBox9.Text = parts(17)
    197. If parts(18).ToString = "True" Then
    198. Main.CheckBox1.Checked = True
    199. Else
    200. End If
    201. If parts(19).ToString = "Frei" Then
    202. Main.RadioButton1.Select()
    203. Else
    204. Main.RadioButton2.Select()
    205. End If
    206. Main.RichTextBox13.Text = parts(20)
    207. Main.RichTextBox11.Text = parts(21)
    208. Main.TextBox4.Text = parts(22)
    209. Main.TextBox8.Text = parts(23)
    210. Main.TextBox2.Text = parts(24)
    211. Main.TextBox5.Text = parts(25)
    212. Main.TextBox6.Text = parts(26)
    213. Main.TextBox7.Text = parts(27)
    214. Main.TextBox3.Text = parts(28)
    215. Main.ComboBox21.SelectedIndex = -1
    216. Main.ComboBox21.SelectedText = parts(29).ToString
    217. Main.ComboBox16.SelectedIndex = -1
    218. Main.ComboBox16.SelectedText = parts(30).ToString
    219. Main.ComboBox20.SelectedIndex = -1
    220. Main.ComboBox20.SelectedText = parts(31).ToString
    221. Main.ComboBox7.SelectedIndex = -1
    222. Main.ComboBox7.SelectedText = parts(32).ToString
    223. Main.ComboBox9.SelectedIndex = -1
    224. Main.ComboBox9.SelectedText = parts(33).ToString
    225. Main.ComboBox15.SelectedIndex = -1
    226. Main.ComboBox15.SelectedText = parts(34).ToString
    227. Main.ComboBox22.SelectedIndex = -1
    228. Main.ComboBox22.SelectedText = parts(35).ToString
    229. Main.RichTextBox12.Text = parts(36)
    230. Main.RichTextBox10.Text = parts(37)
    231. Main.ComboBox4.SelectedIndex = -1
    232. Main.ComboBox4.SelectedText = parts(38).ToString
    233. Main.TextBox12.Text = parts(39)
    234. Dim part12 = parts(40).ToString.Split(" "c)
    235. Dim tripple2 = part12(0).Split("."c)
    236. Dim EndDate2 As String = tripple2(0) + "/" + tripple2(1) + "/" + tripple2(2)
    237. ValCon2 = Date.ParseExact(EndDate2, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None)
    238. Main.DateTimePicker1.Value = ValCon2
    239. Main.RichTextBox14.Text = parts(41)
    240. Main.RichTextBox15.Text = parts(42)
    241. Main.RichTextBox16.Text = parts(43)
    242. Dim part13 = parts(44).ToString.Split(" "c)
    243. Dim tripple3 = part13(0).Split("."c)
    244. Dim EndDate3 As String = tripple3(0) + "/" + tripple3(1) + "/" + tripple3(2)
    245. Valcon3 = Date.ParseExact(EndDate3, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None)
    246. Main.DateTimePicker3.Value = Valcon3
    247. Main.ComboBox2.SelectedIndex = -1
    248. Main.ComboBox2.SelectedText = parts(45).ToString
    249. Catch Ex As Exception
    250. MsgBox("Fehler beim abfüllen der Daten")
    251. MsgBox(Ex.ToString)
    252. End Try
    253. End Sub
    254. End Class
    Metal-Schweiz wurde nun offiziell veröffentlich nach all den Jahren :)

    Orion schrieb:

    habe ich immer Fehler mit dem Datumsformat
    in welcher Zeile Deines Programms?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    Orion schrieb:

    möchte ich euch fragen wo und wie ich korrekt Fehler abfangen kann
    Das kommt doch auf den Fehler an - welchen Fehler meinst du? (bitte mit Zeile und Fehlermeldung - Exceptions, und was sie uns sagen wollen)



    Orion schrieb:

    Try-Catch ging nicht,...
    aber sind doch TryCatchens drinne (Sogar ziemlich grausige).



    Generell gilt: Programmier halt so, dass keine Fehler auftreten.
    Etwa wenn du die Daten selbst schreibst, dann ist auch 100% sicher möglich, dass du die auch fehlerfrei wieder einlesen kannst.



    Übrigens: Du sitzt dem 3.Deppen-Feature von vb.net auf, indem du Main wie ein Objekt benutzst, obwohls ein Typ ist: Instanziierung von Forms und Aufruf von Dialogen
    Die Try-Catchs die jetzt drin sind hab ich nur um die Fehler zu finden im allgemeinen. Die geb ich mir am liebsten mit MsgBoxen aus weil der Eventlog voll ist mit nicht zugewiesenen Werten und so weiter. In Zeile 151 bekomm ich den ersten Fehler das der Index des Arraybereichs überschritten ist weil vermutlich irgendwo kein Wert eingegeben wurde aber diese Werte werden eben vom User schlussendlich eingegeben und sollen keine Pflichtfelder sein, darum muss das irgendwie übersprungen werden wenn der Wert nicht korrekt ist. Da man ein Formular ausfüllt und dann im Nachhinein der Kunde mit seinem Kunden das Formular bespricht und von Hand erweitert und so weiter.
    EDIT: Das mit dem Deppenfeature weiss ich... Das Programm ist ein altes Projekt und muss von mir nur angepasst werden innerhalb kürzester Zeit grosse Korrekturen passen da nicht rein. Zukünftige Programme und Codes sehen ganz sicher nicht so aus ^^
    Metal-Schweiz wurde nun offiziell veröffentlich nach all den Jahren :)

    eventlog - was ist das, und was hast du damit zu schaffen?

    Mach die Trycatchens einfach weg, dann kriegst du auch vernünftige Fehlermeldungen.
    Und kannst dann auch ganz einfach und eindeutig Rods und meine Frage nach Fehlermeldung und Fehlerzeile beantworten.

    Zu Zeile#151: Also TryCatch wegmachen, und im Lokal-Fenster gucken, warum Triple2 nicht so viele Elemente hat, als wie du dachtest.
    Sowas wird hier gezeigt: VisualStudio richtig nutzen

    Orion schrieb:

    um die Fehler zu finden
    zu verschleiern.
    So findest Du gar nix.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Ich weiss warum es nicht so viele Elemente hat wie es sollte. Es geht mir ja darum solche Fehler zu überspringen weil das Feld kein Pflichtfeld sein soll. Gibt es da nicht so eine Möglichkeit wie zum Beispiel mit If Abfragen den Wert auf Gültigkeit zu prüfen und dann zu ersetzen mit einem Leerschlag oder so ähnlich wenn das Feld ungültig ist?
    Metal-Schweiz wurde nun offiziell veröffentlich nach all den Jahren :)

    Orion schrieb:

    Es geht mir ja darum
    hier was zu lernen.
    Jou.
    Fang an und schmeiß zunächst alle Try / Catches raus. Dann kümmern wir uns um jeden einzelnen Fehler. :thumbup:
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Okay, hab dank eurer Hilfe schon alle Fehler behoben. Hatte nur wieder mal ein Brett vorm Kopf weil ich jetzt seit längerer Zeit nicht mehr(oder nur Java) programmiert hab. Aber wäre ja ganz einfach gewesen.
    Metal-Schweiz wurde nun offiziell veröffentlich nach all den Jahren :)