• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 567
  • Last Modified:

VB.NET - Catch all Thread exceptions, even exceptions in newly created threads.

I was wondering if anyone knew how to catch global exceptions in a VB.NET windows form application. Here is what I have tried already:

AddHandler System.Windows.Forms.Application.ThreadException, AddressOf HandleException

This works great for exceptions thrown within the main form's thread.

However, if I do something like the following:

Sub TheThread()
    'code that causes exception.
End Sub

then inside my .net form app, I create a button that initiates the thread like so:

Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click

    Dim oThread as Threading.Thread = New Threading.Thread(AddressOf TheThread)

End Sub

the application will actually crash and exception will not get "Handled".


My Goal is to be able to output a memory dump of the exception causing the error on the main form thread and any thread being started by the form out to a file on the system for reporting later.

Thanks in advance for any assistance on this.
  • 4
  • 2
1 Solution
Well there is the Application Framework that catches unhandled exceptions at the global level.
Have you tried that?
mrgloverAuthor Commented:
Can you elaborate on that? Not sure I have come across this.
mrgloverAuthor Commented:
I actually figured it out.

Some of the examples online were pointing to:

        AddHandler System.Windows.Forms.Application.ThreadException, AddressOf HandleFormException

for form applications and:

        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf HandleThreadException

for console applications.

However, the second of the two is whats really necessary to catch those rouge thread exceptions and the first is the one that will catch the exceptions invoked on the Main form thread.

So in short, add both, and you should be set... I am no expert on this by any means, I just know what worked for me.

Thanks for your time.
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Inside "My Project"
Application tab

click "View Application Events" button

choose, My Application Events from the dropdown

click on UnhandledException

This event should get fired for all unhandled exceptions and is a way to do global error handling as you described.
mrgloverAuthor Commented:
Great, that works too! Thanks a lot for your help!
mrgloverAuthor Commented:
Final Solution was accurate. The solution I presented works as well, and probably for difference uses. Thanks again!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now