I know that I am a little late ... just got home.
As OriginalGriff stated, you can't catch all circumstances, but there are many that you can. Add the following to your
Application
class:
Class Application
Protected Overrides Sub OnStartup(e As StartupEventArgs)
AddHandler Current.DispatcherUnhandledException,
New DispatcherUnhandledExceptionEventHandler(AddressOf AppDispatcherUnhandledException)
AddHandler Dispatcher.UnhandledException,
New DispatcherUnhandledExceptionEventHandler(AddressOf DispatcherOnUnhandledException)
AddHandler AppDomain.CurrentDomain.UnhandledException, _
AddressOf CurrentDomainOnUnhandledException
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)
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)
Dim ex = TryCast(e.ExceptionObject, Exception)
MessageBox.Show(ex.Message,
ex.TargetSite.ToString(),
MessageBoxButton.OK,
MessageBoxImage.[Error])
Current.Shutdown()
End Sub
Protected Overrides Sub OnExit(e As ExitEventArgs)
MyBase.OnExit(e)
End Sub
End Class