see
http://www.wintellect.com/resources/newsletters/articles/appsdie.aspx"...
WinForms Applications
WinForms application exception handling has a twist in it. Exceptions for the main thread go to a different handler than exceptions from the other threads. You'll need to add a delegate to the Application.ThreadException member that has the type System.Threading.ThreadExceptionEventHandler in order to handle them. If you want notification of exceptions from other threads, you'll have to also set the AppDomain.CurrentDomain.UnhandledException event, as I discussed in the previous section.
..."
http://www.wintellect.com/resources/newsletters/articles/appsdie.aspx"...
WinForms Applications
WinForms application exception handling has a twist in it. Exceptions for the main thread go to a different handler than exceptions from the other threads. You'll need to add a delegate to the Application.ThreadException member that has the type System.Threading.ThreadExceptionEventHandler in order to handle them. If you want notification of exceptions from other threads, you'll have to also set the AppDomain.CurrentDomain.UnhandledException event, as I discussed in the previous section.
..."
Module Module1 Sub main()
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf MyException
Application.Run(new myForm)
End SubSub MyException(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs)
MessageBox.Show("Unhandled Exception!" & ControlChars.CrLf & args.ExceptionObject.GetType().ToString(), "Exceptions")
End Sub
End ModulePrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Throw (New System.Exception)
'我也试过:
'Throw (New System.ExecutionEngineException)
End sub
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf MyException
AddHandler Application.ThreadException, AddressOf YourThreadExceptionEventHandler
Application.Run(new myForm)
End SubSub MyException(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs)
MessageBox.Show("Unhandled Exception!" & ControlChars.CrLf & args.ExceptionObject.GetType().ToString(), "Exceptions")
End Sub
Sub YourThreadExceptionEventHandler(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs)
MessageBox.Show(e.Exception.Message)
end Sub
End Module
有的也写到系统日值文件里面;
用Application.ThreadException可以,在调试和运行时的效果是一样的。而AppDomain.CurrentDomain.UnhandledException在调试时可以,在独立运行则不行。