Google
WWW Yariv Hammer's Code Site

Thursday, January 04, 2007

A Deployment Issue: "the application has generated an exception that could not be handled."

Introduction
I built an application on my computer using .NET Framework 1.1, and it worked. This was a complicated application with a bunch of exe and dll files, app.config files, xml files. I had a server, clients, an MS-Access mdb file. I had GUI libraries, and Data access libraries.

I supplied an installation file, that installed .NET Framework 1.1, MDAC 2.8, my application, and several MFC files. It worked on other machines as well.

Then I had an update. I created a new installation file and shiped it to another computer.
One of the .NET applications crashed, and gave me the very intimidating MessageBox: "the application has generated an exception that could not be handled. Process id=0xFFFF Thread id=0xFFFF. Press OK to terminate the application, and CANCEL to debug the application.".

Of course I had no debugger on that machine, and basically I was stuck!

First Tries
At first I thought it was a .NET Framework installation issue. So I uninstalled .NET Framework on the other computer, and re-installed it. It did not solve the issue. I checked the versions of the .NET Framework (using Administrative Tools), and it was the same as in my computer.

Second, I checked that the App.Config file existed oin the folder of installation. It was there.

Third, I installed Dependency Walker on the other computer, open my exe with it (no red signs appeared), pressed F7, but could not make sense of the output. It did not smell like a problem with dependencies or other dlls.

The Solution
I added to the application the following code (I made it start from the Main method):
-------------------------------------------------
<STAThread()> _
Public Shared Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
AddHandler currentDomain.UnhandledException, AddressOf UnhandledExceptionOccured
Application.Run(New Form1)
End Sub

Public Shared Sub UnhandledExceptionOccured(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
Dim ex As Exception = CType(e.ExceptionObject, Exception)
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace)
End Sub
-------------------------------------------------


This catches any Unhandled Exceptions, and shows a message with the stack trace.

I compiled and installed this exe in the client's computer.

I was amazed: There was a bug involving DBNull in one of my dlls, a bug that I have just added to my application. I could have never guessed that the problem was in that place!

My Lesson
From now on I put this code in all my applications! When there is such a bug I must at least know what happened!

I advice you to do the same.

Labels: , , ,

0 Comments:

Post a Comment

<< Home

Feel free to use everything here. Add links to my site if you wish.

Do not copy anything to other sites without adding link to here.

All the contents of the site belong to Yariv Hammer.