Graeme_Grant
Я знаю, что немного опоздал ... только что вернулся домой.
Как сказал OriginalGriff, вы не можете поймать все обстоятельства, но есть много, что вы можете. Добавьте в свой список следующее Application
класс:
Class Application
Protected Overrides Sub OnStartup(e As StartupEventArgs)
' setup global exception handling
AddHandler Current.DispatcherUnhandledException,
New DispatcherUnhandledExceptionEventHandler(AddressOf AppDispatcherUnhandledException)
AddHandler Dispatcher.UnhandledException,
New DispatcherUnhandledExceptionEventHandler(AddressOf DispatcherOnUnhandledException)
AddHandler AppDomain.CurrentDomain.UnhandledException, _
AddressOf CurrentDomainOnUnhandledException
' start the app
MyBase.OnStartup(e)
End Sub
Private Sub AppDispatcherUnhandledException(sender As Object, _
e As DispatcherUnhandledExceptionEventArgs)
ForwardUnhandledException(e)
End Sub
Private Sub DispatcherOnUnhandledException(sender As Object, _
e As DispatcherUnhandledExceptionEventArgs)
ForwardUnhandledException(e)
End Sub
Private Sub ForwardUnhandledException(e As DispatcherUnhandledExceptionEventArgs)
' forward the exception to AppDomain.CurrentDomain.UnhandledException ...
Current.Dispatcher.Invoke(DispatcherPriority.Normal,
New Action(Of Exception)(Sub(exc)
Throw New Exception("Exception from another Thread", exc)
End Sub), e.Exception)
End Sub
Private Sub CurrentDomainOnUnhandledException(sender As Object, _
e As UnhandledExceptionEventArgs)
' Do logging of exception details
' Let the user know that something serious happened...
Dim ex = TryCast(e.ExceptionObject, Exception)
MessageBox.Show(ex.Message,
ex.TargetSite.ToString(),
MessageBoxButton.OK,
MessageBoxImage.[Error])
' ask the app to shut down...
Current.Shutdown()
End Sub
Protected Overrides Sub OnExit(e As ExitEventArgs)
' last change for cleanup code here!
' clear to exit app
MyBase.OnExit(e)
End Sub
End Class