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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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.
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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.

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
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!
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
.NET Programming

From novice to tech pro — start learning today.