Solved

Handling an 'unhandled exception'

Posted on 2006-07-04
9
328 Views
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

    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)

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

stubar
0
Comment
Question by:stuartbarber
9 Comments
 
LVL 9

Expert Comment

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

                Try

                    frmTagFindAndReplaceOBJ = New frmTagFindAndReplace

                    Application.Run(frmTagFindAndReplaceOBJ)

                Catch ex As Exception

                    MsgBox(ex.ToString)
                End Try

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

sub main
                Try
                CallStartingSub
                Catch ex As Exception
                MsgBox(ex.ToString)
                End Try
end sub

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

Accepted Solution

by:
jrscherer earned 43 total points
ID: 17037555
Hi Lojk,
This blog explains part of your problem.
http://www.codinghorror.com/blog/archives/000201.html
Jack.net
0
 
LVL 1

Author Comment

by:stuartbarber
ID: 17037622
lojk

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

I am using Visual Studio 2005.

Cheers

stubar
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Assisted Solution

by:ptakja
ptakja earned 41 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...
0
 
LVL 9

Assisted Solution

by:lojk
lojk earned 41 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)

Stubar,

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...

0
 
LVL 9

Expert Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now