Results 1 to 5 of 5

Thread: [RESOLVED] Global Error Handler

  1. #1

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Resolved [RESOLVED] Global Error Handler

    Hi All.

    I want to create like a global error handler.

    Whereever an error in code appears, I want it to open a form. (FrmErrorHandler) Then in this form, it should put the error in a textbox.

    This I then want to save with a few other variables (app version, workstation, username etc etc) in my sql server.

    The only thing the user needs to see, is the form that tells him that an error occurred (almost like that anoying send / don't send forms of windows xp)

    How do I say for it to open the form no matter what the error is (sql server, my application, user input)

    How do I insert the error code, in the form?

    Thanks
    Rudi

  2. #2
    Hyperactive Member The_Duck's Avatar
    Join Date
    May 2005
    Location
    Leamington, UK
    Posts
    351

    Re: Global Error Handler

    To handle any application error I add a handler for the Application.ThreadException in my startup routines as follows

    AddHandler Application.ThreadException, AddressOf ApplicationException

    Private Sub ApplicationException(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
    ErrorMsg(e.Exception)
    End Sub

    Note that I would still highty recommend using Try, Catch, Finally, End Try where you are likely to come across errors, such as when connecting to a database. Relying on the Application.ThreadException to deal with and handle your exceptions is a very bad idea!

    If you create a global Function ErrorMsg(ByVal e as Exception)

    Then you can have your function do what you want, ie. insert the error into a SQL database.

    Hope this helps

  3. #3

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Global Error Handler

    Hey Mr Duck....

    I think I'll go the global Module route....

    What kind of code would I have to add to each form to capture these errors? I mean, what code would I need to add to forms to run the module?

    And do you have and sample code or examples to give ideas?

    Thanks

    Rudi

  4. #4
    Hyperactive Member The_Duck's Avatar
    Join Date
    May 2005
    Location
    Leamington, UK
    Posts
    351

    Re: Global Error Handler

    Say you wanted to capture any unhandled errors in your Form myForm

    You could use something like the following

    VB Code:
    1. Try
    2.    Dim frm AS new myForm()
    3.    frm.ShowDialog()
    4. Catch ex As Exception
    5.    ErrorMsg(ex)
    6. End Try
    where you have ErrorMsg defined as
    VB Code:
    1. Public Sub ErrorMsg(ByVal ex AS Exception)
    2.     MessageBox.Show("Error! " & ex.ToString)
    3. End Sub

  5. #5

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Global Error Handler

    I was actually speaking of opening a form... but ok I got the idea, thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width