How to pass exception aspx location to error class

I have written an exception class to log my exceptions and created a cookie to store the name of the page that the exception originated from and then displaying it in the errors redirect page.  Is there a simpler way to accomplish this.  Can I create the cookie or some other method to store the page name containing the exceptionr in the exceptionr class.  I have tried but can't get this to work.  What I have done does work but I am not sure sure this is the best way to handle this.  Any advice would be appreciated.  Thank-you.
            Catch ex As Exception
            Dim myerror As ErrorsClass
            myerror = New ErrorsClass(ex.Message, ex.Source, ex.StackTrace)
            System.Web.HttpContext.Current.Response.Cookies("PageFrom").Value = Request.ServerVariables("url")
Public Class ErrorsClass
    'declare Message, Source and StackTrace
    Inherits Exception
    Private _exceptionMessage As String
    Private _exceptionSource As String
    Private _exceptionStackTrace As String
    'create overrideable Readonly property for exception message
    Public Overridable ReadOnly Property exceptionMessage() As String
            Return _exceptionMessage
        End Get
    End Property
    'pass message, source and stacktrace when new errors class is created
    Public Sub New(ByVal Message As String, ByVal Source As String, ByVal StackTrace As String)
        _exceptionMessage = Message
        _exceptionSource = Source
        _exceptionStackTrace = StackTrace
    End Sub
    'write error to text file
    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("Exception Source= " & _exceptionSource)
        file.WriteLine("Exception Message= " & _exceptionMessage)
        file.WriteLine("Exception StackTrace= " & _exceptionStackTrace)
    End Sub
End Class
<form id="Form1" style="FONT-SIZE: 11pt; FONT-FAMILY: Arial" method="post" runat="server">
An error has occurred on 
<%Response.write(Request.Cookies("PageFrom").value)%>. Please contact the Helpdesk at local 6706.<br>
			<A href="NavigationNET.aspx">Online Forms Home Page</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<A href="NavigationNET.aspx?Status=CANCEL">Cancel</A>

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

are you using the customErrors setting in web.config?
If yes, then .Net will automatically redirect to the error page when an error occurs, and automatically add a querystring variable named "ErrorPage" to the error page.
This is an extremely simple example of using an html page as an error page, and still using the querystring variable set by .Net

if instead of an html page, you set the webconfig to use your ErrorsRedirectPage.aspx page, you can instantiate and use your class on that page.  that way, you don't have to instantiate your error class in every try/catch...
or another common approach is to use the global.asax page to instantiate and handle your error class...
If you look at listing 2, it does some of what your class does...but the key is that you can manage all errors using global.asax, and not have to have it in every try/catch

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lnshopAuthor Commented:
Thank-you very much for your help.  I need to log the errors to a text file so using creating a custom class seemed to be the solution.  I have tried the default redirect and could not get this to work.  I am only using the try catch in pages that contain database connections so it was not necessary to use the the global asax.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.