Capturing unhandled exceptions

A

Alex Glass

I am using a reference to a Library called MySql.Data in my application. If
I run my program on a computer that does not have the library installed, the
application throws an exception and a window pops up that says: "Application
has generated an exception that could not be handled". Is there any way to
capture this type of exception. I have tried using Try Catch blocks and
also the UnhandledException/UnhandledThreadException events without any
luck. Below is some code to clarify the problem.

The software I am using is can be found at
http://dev.mysql.com/downloads/connector/net/1.0.html

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

Module Startup
Public Sub Main()
Try
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf
UnhandledException
AddHandler Application.ThreadException, AddressOf
ThreadException

MsgBox("Creating Object")
Dim obj As MySql.Data.MySqlClient.MySqlConnection = New
MySql.Data.MySqlClient.MySqlConnection
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub UnhandledException(ByVal sender As Object, ByVal e As
UnhandledExceptionEventArgs)
MsgBox("An unhandled exception has occurred")
End Sub

Private Sub ThreadException(ByVal sender As Object, ByVal e As
Threading.ThreadExceptionEventArgs)
MsgBox("An unhandled exception has occurred")
End Sub
End Module

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

On the computer that does not have MySql.Data installed the program bypasses
the exception handlers and just displays the standard unhandled exception
window. If the line has no New statement the program runs without a
problem.
As in:
Dim obj As MySql.Data.MySqlClient.MySqlConnection

I suppose I could resort to using CreateObject but I would prefer to avoid
this if possible.
 
M

Matt Berther

Hello Alex,

Im guessing that the exception is actually happening prior to your application's
Main method being called. The assemblies need to be bound up and this occurs
when the program is JIT'd. My description my not be completely accurate,
but the concept is. I'm sure that people will let me know the correct terminology.

Now, the way that you could get around this is one of two ways:

1. Develop an intermediate assembly that both your application and MySql.Data
reference that defines an interface and a factory class. At this point, you
can program against the interface and since your application will include
this intermediate assembly, it will be able to start just fine. However,
do keep in mind that you will probably need to use latebinding in this assembly
(Activator.CreateInstance vs new) or you could run into the same problem
again.

2. Create a bootstrap application that checks for any required components
prior to starting the actual application. A simple example might be:

public class BootStrapper
{
public static void Main(string[] args)
{
try
{
Type type = Type.GetType("MySql.Data.MySqlClient.MySqlConnection",
true);
Process.Start("YourApplication.exe");
}
catch (TypeLoadException)
{
MessageBox.Show("An error was encountered loading the MySql.Data
library. Please check your installation.");
}
}
}

I apologize that the code is not VB, but Im sure you get the gist of it...
 
D

David Levine

Try moving the code that references the external lib into a method other
then main. The granularity of the JIT is per-method, and it will compile the
Main method before you enter it and can register the UE handler. That should
give you a chance to wrap the other method that actually loads the library
with a try-catch handler.
 
A

Alex Glass

Thanks for the help David, now I actually understand what is happening. So
long as my New object code is place in a separate method from Main() things
work exactly as I would have expected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top