lnshop
asked on
How can I create a custom exception class in vb.net
I am using vb.net 2003 to create a custom exception handling class. The code that I am currently using works fine but I will need to use this exception handling pages containing data connections, queries, etc. and would like to just access a custom class. Here is a short version of the code that is working and the code snippet attached is what I have in the class so far. The line ex.message.tostring is not working. Any help or direction on how to accomplish this would be appreciated. Thank-you very much.
Try
Catch ex As Exception
'WORKING CODE
'Dim file As New System.IO.StreamWriter(Ser ver.MapPat h("..\Erro rLog.txt") , True)
'file.WriteLine(FormatDate Time(Date. Now, DateFormat.GeneralDate))
'file.WriteLine(ex.Message .ToString( ))
'file.WriteLine(ex.Source. ToString() )
'file.WriteLine(ex.StackTr ace.ToStri ng())
'file.Close()
'Response.Redirect("http://localhost/NetTestDocs/ErrorsRedirectPage.aspx")
'END OF CODE
Finally
conn.Close()
conn.Dispose()
End Try
End Sub
Try
Catch ex As Exception
'WORKING CODE
'Dim file As New System.IO.StreamWriter(Ser
'file.WriteLine(FormatDate
'file.WriteLine(ex.Message
'file.WriteLine(ex.Source.
'file.WriteLine(ex.StackTr
'file.Close()
'Response.Redirect("http://localhost/NetTestDocs/ErrorsRedirectPage.aspx")
'END OF CODE
Finally
conn.Close()
conn.Dispose()
End Try
End Sub
Public Class ErrorsClass
Public ex As Exception
Sub writererror()
'Dim file As New System.IO.StreamWriter(Server.MapPath("..\ErrorLog.txt"), True)
Dim file As New System.IO.StreamWriter(HttpContext.Current.Server.MapPath("..\ErrorLog.txt"), True)
file.WriteLine(FormatDateTime(Date.Now, DateFormat.GeneralDate))
'file.WriteLine(ex.Message.ToString())
file.Close()
End Sub
End Class
you can also create your own Exception, by crearing a class the inherits from Exception:
Public Class MyException Inherits Exception
Private _exceptionMessage as String
Public ReadOnly Property ExceptionMessage() as String
Property Get
Return _exceptionMessage
End Get
End property
Public Sub New(ByVal Message as String)
MyBase.New("Custom Error")
_exceptionMessage = Message
End Sub
End Class
Then you can use it like this:
Try
If someCondition then
Throw New MyException("Something dreadful has happened")
End if
Catch ex as MyException
MessageBox.Show(ex.ExceptionMessage, vbOkOnly
Finally
End try
ASKER
Thank-you so much for your help. I have the error message working now but I am having a problem adding the error source. Attached is the class code. How can I add the error source to the text file. I have tried adding source as a property and have tried adding innerException and can't get this to work. If you could help me with adding the exception source, I would appreciate it very much.
'CODE IN VB CODE BEHIND
Catch ex As Exception
'REFERENCE TO ERRORS CLASS
Dim myerror As ErrorsClass
myerror = New ErrorsClass(ex.Message)
myerror.writererror()
'CODE IN VB CODE BEHIND
Catch ex As Exception
'REFERENCE TO ERRORS CLASS
Dim myerror As ErrorsClass
myerror = New ErrorsClass(ex.Message)
myerror.writererror()
'ERRORS CLASS
Imports System.Exception
Public Class ErrorsClass
Inherits Exception
Private _exceptionMessage As String
Public ReadOnly Property exceptionMessage() As String
Get
Return _exceptionMessage
End Get
End Property
Public Sub writererror()
Dim file As New System.IO.StreamWriter(HttpContext.Current.Server.MapPath("..\ErrorLog.txt"), True)
file.WriteLine(FormatDateTime(Date.Now, DateFormat.GeneralDate))
file.WriteLine(Message.ToString())
file.Close()
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank-you very much. That works great. I have the source and stack trace being written to the error log now.
Glad to be of assistance
AW
AW
An example here:
http://www.vbdotnetheaven.com/UploadFile/susanabraham/CustomExceptionHandlingvb11122005021337AM/CustomExceptionHandlingvb.aspx?ArticleID=4ce8bdfe-1a63-44da-a4a3-f8e796d63302
When you say that ex.Message.ToString() is "not working", do you mean that it's returning an empty string?
If you replace these two lines:
'file.WriteLine(ex.Message
'file.WriteLine(ex.Source.
with
'file.WriteLine(ex.ToStrin
does it include the expected message? (Wishful thinking here)