By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,572 Members | 1,516 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,572 IT Pros & Developers. It's quick & easy.

Problem with Application.ThreadException

P: n/a
My Problem is that although i have added a handler to
Application.ThreadException and it works fine for my application exceptions,
it does not fire if an exception is generated inside an event handler of my
App.

eg. If i throw a exception inside DragDrop, or MouseDown of an event the
ThreadException does not fire !!

Any ideas ???

Thanks in advance
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I just threw an exception in MouseDown of my form and my global error
handler code caught it in the Application.ThreadException.

Here is my app.vb module if you want to compare notes: (sorry about the
mess)

HTH,
Greg

Option Strict On

Imports System.Security

Module App

'The main entry point for the application.
<STAThread()> Public Sub Main()

Try
'Demand full trust permissions
Dim fullTrust As New
PermissionSet(Permissions.PermissionState.Unrestri cted)
fullTrust.Demand()

SubMain()
Catch ex As SecurityException
' Report that permissions were not full trust
MessageBox.Show("This application requires full-trust security
permissions to execute.")

Catch ex As Exception
HandleUnhandledException(ex)
End Try

End Sub
Private Sub SubMain()

' // Setup unhandled exception handlers
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf
OnUnhandledException
'AppDomain.CurrentDomain.UnhandledException += // CLR
' new UnhandledExceptionEventHandler(OnUnhandledExceptio n);

AddHandler Application.ThreadException, AddressOf
OnGuiUnhandedException
'Application.ThreadException += // Windows Forms
' new System.Threading.ThreadExceptionEventHandler(
' OnGuiUnhandedException);
'Application.EnableVisualStyles()
Application.Run(New Main)
End Sub
'// CLR unhandled exception
Private Sub OnUnhandledException(ByVal sender As Object, ByVal e As
UnhandledExceptionEventArgs)
HandleUnhandledException(e.ExceptionObject)
End Sub

' // Windows Forms unhandled exception
Private Sub OnGuiUnhandedException(ByVal sender As Object, ByVal e As
System.Threading.ThreadExceptionEventArgs)
HandleUnhandledException(e.Exception)
End Sub
Private Sub HandleUnhandledException(ByVal o As Object)
'Dim e As Exception = CType(o, Exception)

'If Not (e Is Nothing) Then ' // Report System.Exception info
' Debug.WriteLine("Exception = " + e.GetType());
' Debug.WriteLine("Message = " + e.Message);
' Debug.WriteLine("FullText = " + e.ToString());
'Else ' // Report exception Object info
' Debug.WriteLine("Exception = " + o.GetType());
' Debug.WriteLine("FullText = " + o.ToString());
'End If

MessageBox.Show("An unhandled exception occurred and the application
is shutting down.")
Environment.Exit(1) ' // Shutting down

End Sub
End Module

"Alex" <al********@yahoo.gr> wrote in message
news:32**************************@posting.google.c om...
My Problem is that although i have added a handler to
Application.ThreadException and it works fine for my application exceptions, it does not fire if an exception is generated inside an event handler of my App.

eg. If i throw a exception inside DragDrop, or MouseDown of an event the
ThreadException does not fire !!

Any ideas ???

Thanks in advance

Nov 20 '05 #2

P: n/a
Well Greg, first of all thanks for your response.
I used your code in my app, and yes an error on mouse down really
raises the threadexception event, but when i throw an exception from
the DragDrop event of a treeview... guess what.... nothing happens !!!

What is going on here ?


"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message news:<#T**************@tk2msftngp13.phx.gbl>...
I just threw an exception in MouseDown of my form and my global error
handler code caught it in the Application.ThreadException.

Here is my app.vb module if you want to compare notes: (sorry about the
mess)

HTH,
Greg

Option Strict On

Imports System.Security

Module App

'The main entry point for the application.
<STAThread()> Public Sub Main()

Try
'Demand full trust permissions
Dim fullTrust As New
PermissionSet(Permissions.PermissionState.Unrestri cted)
fullTrust.Demand()

SubMain()
Catch ex As SecurityException
' Report that permissions were not full trust
MessageBox.Show("This application requires full-trust security
permissions to execute.")

Catch ex As Exception
HandleUnhandledException(ex)
End Try

End Sub
Private Sub SubMain()

' // Setup unhandled exception handlers
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf
OnUnhandledException
'AppDomain.CurrentDomain.UnhandledException += // CLR
' new UnhandledExceptionEventHandler(OnUnhandledExceptio n);

AddHandler Application.ThreadException, AddressOf
OnGuiUnhandedException
'Application.ThreadException += // Windows Forms
' new System.Threading.ThreadExceptionEventHandler(
' OnGuiUnhandedException);
'Application.EnableVisualStyles()
Application.Run(New Main)
End Sub
'// CLR unhandled exception
Private Sub OnUnhandledException(ByVal sender As Object, ByVal e As
UnhandledExceptionEventArgs)
HandleUnhandledException(e.ExceptionObject)
End Sub

' // Windows Forms unhandled exception
Private Sub OnGuiUnhandedException(ByVal sender As Object, ByVal e As
System.Threading.ThreadExceptionEventArgs)
HandleUnhandledException(e.Exception)
End Sub
Private Sub HandleUnhandledException(ByVal o As Object)
'Dim e As Exception = CType(o, Exception)

'If Not (e Is Nothing) Then ' // Report System.Exception info
' Debug.WriteLine("Exception = " + e.GetType());
' Debug.WriteLine("Message = " + e.Message);
' Debug.WriteLine("FullText = " + e.ToString());
'Else ' // Report exception Object info
' Debug.WriteLine("Exception = " + o.GetType());
' Debug.WriteLine("FullText = " + o.ToString());
'End If

MessageBox.Show("An unhandled exception occurred and the application
is shutting down.")
Environment.Exit(1) ' // Shutting down

End Sub
End Module

"Alex" <al********@yahoo.gr> wrote in message
news:32**************************@posting.google.c om...
My Problem is that although i have added a handler to
Application.ThreadException and it works fine for my application

exceptions,
it does not fire if an exception is generated inside an event handler of

my
App.

eg. If i throw a exception inside DragDrop, or MouseDown of an event the
ThreadException does not fire !!

Any ideas ???

Thanks in advance

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.