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:

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)
        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?

Recommended Answers

All 2 Replies


I should use a Class for this so that you can call it instead of writing the whole code over again.

Yes write it in modlue level. so that all the methods in all the form can use it..
Log the Stracktrace in textfile so that developer will come to know where the error is..

I heard using try catch finally block everywhere is expensive... so do some R&D on try catch and use accordingly..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.