PC Review


Reply
Thread Tools Rate Thread

Capturing unhandled exceptions

 
 
Alex Glass
Guest
Posts: n/a
 
      8th Feb 2005
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.


 
Reply With Quote
 
 
 
 
Matt Berther
Guest
Posts: n/a
 
      8th Feb 2005
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...

--
Matt Berther
http://www.mattberther.com

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




 
Reply With Quote
 
David Levine
Guest
Posts: n/a
 
      8th Feb 2005
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.

"Alex Glass" <(E-Mail Removed)> wrote in message
news:cvYNd.6515$(E-Mail Removed)...
>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.
>



 
Reply With Quote
 
Alex Glass
Guest
Posts: n/a
 
      8th Feb 2005
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unhandled Exceptions Christoph Basedau Microsoft C# .NET 2 2nd Jun 2010 12:59 PM
Capturing Unhandled exceptions Kevin S Gallagher Microsoft VB .NET 4 27th Jun 2007 10:16 PM
Unhandled Exceptions Andreas van de Sand Microsoft C# .NET 3 13th Sep 2006 05:14 PM
Unhandled Exceptions - ASP.Net tberry@vhb.com Microsoft ASP .NET 1 7th Mar 2006 10:41 PM
Unhandled Exceptions outside of VS =?Utf-8?B?QnJ5Y2UgQ292ZXJ0?= Microsoft Dot NET Framework Forms 1 5th Jul 2005 08:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:12 AM.