I'm looking for any thoughts on how I am currently doing error handling in my VB.NET programs. Basically I put code in a try/catch, then call an error handling sub passing the exception and a string. The error handling sub gives an error message and writes to a text file:
Try do some stuff Catch ex As Exception errorArg = "This failed because of something." errorHandle(errorArg, ex) Sub errorHandle(ByVal errorArg As String, ByVal ex As System.Exception) Dim errorFile As String = "C:\dev\temp\errorfile.txt" Dim errorWrite As New IO.StreamWriter(errorFile, True) Dim machname As String = System.Environment.MachineName.ToString Dim close As DialogResult = MessageBox.Show(errorArg & vbCrLf & "Contact MIS", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error) errorWrite.Write("*** ERROR on " & curDate & " @ " & machname & vbCrLf & errorArg & vbCrLf & ex.ToString & vbCrLf) errorWrite.Close() If close = DialogResult.OK Then End End Sub
As you can see I'm displaying the 'custom' error message to user and writing it to an error file along with the exception data. I've been using this for a while (it's been successful so far), my question is really more of "should I be doing it this way"? The error handle sub is basically attached to the parent in an MDI or just in the same module if it's single form or console, should I be building a class for this sort of stuff?
Edited by MikeTackett: easier to read