Solved

Capture an unhandled exception

Posted on 2006-07-15
3
390 Views
Last Modified: 2008-02-01
Hi,

I had posted the Q earlier:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21910343.html
and got an answer that was working perfect untill I came across this:

As said earlier,  I put the event handler code in the main form's load event. In the form's load event there is another event handler that calls a sub whenever the binding manager's record changes. I am getting an error in this sub (before the form loads) and that error was unhandled,  I was expecting  the exception handler sub to take care of it. It didn't. I then tried adding a module, putting a sub main and the exception event handler code in it and calling this module at the start of the application (as suggested in the original Q).  It still ends up with the unhandled exception.

I would like to know what modification is needed there?

Thanks,
Gauri

0
Comment
Question by:p_gauri7
3 Comments
 
LVL 14

Expert Comment

by:Ramuncikas
ID: 17116972
I would simulate (if possible) conditions to get error to know what sub raises it. Then, if possible, convert sub to function and put a try...catch block into it. If error occures the function would return false otherwise true (or vice versa). Then you could put a flag depending on function's result.
0
 
LVL 3

Accepted Solution

by:
Pigtor earned 500 total points
ID: 17122320
There are two handlers that must be declared:
The ThreadException doesn't necessary terminate the thread, the UnhandledException is thrown before the application terminates.


1.  Application.ThreadException
This event enables an application to handle an exception intelligently when it receives a thread exception from a window procedure. Attach your event handlers to the ThreadException event to deal with the exception. An appropriate event handler does not terminate the thread, and allow your application to continue executing.

2.  AppDomain.UnhandledException
The UnhandledExceptionEventHandler delegate for this event provides default handling for uncaught exceptions. When this event is not handled, the system default handler reports the exception to the user and terminates the application.


--------------------------------------------------------------------------------------------------------------------------------

Public Sub New()
        MyBase.New()
        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledException
        AddHandler Application.ThreadException, AddressOf UnhandledException

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
End Sub

Private Sub UnhandledException(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs)
     .....
End Sub

Private Sub UnhandledException(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
     .....
End Sub



0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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