Handler Error VB.Net

Every program has error and we meet it again and again sometimes we need to copy exception message and VB 2005 not provide convenient way to copy it.

This article demonstrate how we handler exception while development phase.




1. Create class for contain exception data.


Public Class ExceptionData


Public FileName As String

Public ClassName As String

Public Method As String

Public Section As String

Public Ex As Exception


Sub New(ByVal strFileName As String, ByVal strClassName As String)

Me.FileName = strFileName

Me.ClassName = strClassName


End Sub


End Class



2. Create class for handler Error.

this class is use for create text for display for us.



Public Class ErrorHandler


Public Shared Sub ShowError(ByVal cEx As ExceptionData)


Dim f As New frmShowErrorforDebug

f.ErrorDisplay = GetStringfromException(cEx)


End Sub


Public Shared Function GetStringfromException(ByVal File As String, ByVal ClassName As String, ByVal Method As String, ByVal Section As String, ByVal EX As Exception) As String


Dim strB As New System.Text.StringBuilder


strB.Append(“Error Occur “).Append(vbCrLf).Append(vbCrLf)



strB.Append(“File :”).Append(File).Append(vbCrLf)

strB.Append(“ClassName :”).Append(ClassName).Append(vbCrLf)

strB.Append(“Method :”).Append(Method).Append(vbCrLf)



If Not IsNothing(Section) Then

If Section.Trim <> “” Then

strB.Append(“Section :”).Append(Section).Append(vbCrLf)


End If

End If


strB.Append(“Source :”).Append(EX.Source).Append(vbCrLf)

strB.Append(“Method throw current exception :”).Append(EX.TargetSite.Name).Append(vbCrLf)


strB.Append(“Message :”).Append(EX.Message).Append(vbCrLf)



strB.Append(“Stack Trace :”).Append(EX.StackTrace)


Return strB.ToString


End Function

Public Shared Function GetStringfromException(ByVal cEx As ExceptionData) As String

Return GetStringfromException(cEx.FileName, cEx.ClassName, cEx.Method, cEx.Section, cEx.Ex)



End Function

End Class


3. Create frmShowErrorforDebug


in this case we just use for Windowsform application, this form is just contain one textbox for show error data and two button one for copy error data to clipboard ,two for close form.

If you want to handler error for web application you just change from windows form to webform instead.


Public Class frmShowErrorforDebug

Public ErrorDisplay As String



Private Sub frmShowError_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.TextBox1.Text = errordisplay

End Sub


Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click



End Sub


Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click


End Sub

End Class


Now we already created classes for handler error and then we will test it.



Public Class frmLogin


Private FileName As String = “frmLogin.vb”


Private cEx As New ExceptionData(FileName, “frmLogin”)


Private Sub btnTestError_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestError.Click


cEx.Method = “btnTestError_Click”



Dim str As String = “Hello”

cEx.Section = “Convert str to dbl”

Dim dbl As Double = CDbl(str)


Catch ex As Exception




cEx.Ex = ex





End Try

End Sub

End Class

It done.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: