Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Handling an 'unhandled exception'

Posted on 2006-07-04
Medium Priority
Last Modified: 2008-01-16
I am trying to write a catch-all exception handler in my VB.NET application.

I started out by writing a handler routine and directing the execution to it with

    AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf Me.UnhandledExceptionHandler

This works fine whilst working through Visual Studio but as soon as I publish the app and run it on another machine it doesn't. It simply behaves as usual and reports the error with the option to Quit or Continue.

Can anyone explain this to me?

Alternatively, I started looking at using


but get an error about setting the value before initialising any controls. Can anyone explain where I would set the Unhandled Exception Mode and how I direct it to a user-defined routine.

Many thanks in advance

Question by:stuartbarber
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 17037439
in my sub main on one of my apps i do this...


                    frmTagFindAndReplaceOBJ = New frmTagFindAndReplace


                Catch ex As Exception

                End Try

the principle is the same, even if you dont wont to start a form

sub main
                Catch ex As Exception
                End Try
end sub

I know there are more sophisticated ways to do this with the framework but this works fine too...

Accepted Solution

jrscherer earned 172 total points
ID: 17037555
Hi Lojk,
This blog explains part of your problem.

Author Comment

ID: 17037622

OK, that sounds OK but how do I add the exception handler around the, the main procedure is hidden from me.

I am using Visual Studio 2005.


Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 14

Assisted Solution

ptakja earned 164 total points
ID: 17041799
Try this:

1) Open your project's Properties .
2) Select hte Application tab on the left.
3) Next to the combo for the Spash Screen is a button that says "Application Events". Click it.
    A code window should open up. Line 1 should read "Namespace My"
4) In the left combo above the code (should say "General") select MyApplication Events.
5) In the Right combo, select Unhandled Exception

The editor should create a routine stub that you can fill in with code when you get an unhandled exception...

Assisted Solution

lojk earned 164 total points
ID: 17043865
Jscherer - its not my question. But you are right, that link defiently highlights a quirk in the framework (that i have fortunately never encountered doing it the way i detail above)


VS2005 did change a few bits and bobs from previous .net versions and yes the default is to hide all designer code (i think that was a bit of a MS-take).

In the solution explorer there is, on the top bar, a 'Show All Files' Toggle, that will show you any files related by name or content to the current file.

Now as far as loading the app is concerned..

add a modSomething Module to your project and add a
Public Sub Main
Procedure as i have detailed above.
and in the project properties set the startup object to 'Sub Main'. You may need to switch off 'Application Framework' to do this (you can always do an 'application.visualstyles=' in the submain if you need to)

anyway exactly how much detail do you need on a failed app? A gracefull exit and errorlog.txt is more than enough.. You should be catching these kind of failures way, way, way up the application tree not waiting for them to throw a fatal exception on the entry point...


Expert Comment

ID: 17211034
probably an even split 'tween jscherer,ptaka  and i.?  everything there that stubarber wanted..

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA:…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question