How can I effectively catch exceptions at form level? Is the only solution wrapping all the following ActionN() calls into Try/Catch?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Action1()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Action2()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Action3()
End Sub
Expected design:
have one general exception handler per form (or per all forms) which displays the message, logs the exception etc. (special handlers can exist too, but I'm not discussing them here)
on exiting the handler, control is returned back to the form, which can continue listening for other events
I'm able to do the following, but the handler works everywhere on UI thread except the form :). In the above simple scenario with buttons and I'm getting Unhandled exceptions instead. But I would like to catch the exception before they fall into Unhandled exception handler. (Is going through the Unhandled exception effective way? I feel no.)
Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup
AddHandler Windows.Forms.Application.ThreadException, AddressOf UIThreadException
End Sub
Private Sub UIThreadException(sender As Object, e As ThreadExceptionEventArgs)
MsgBox(e.Exception, vbOKOnly, "[UIThreadException]")
End Sub
What is the more ellegant .NET way to avoid amateurish looking Try/Catch wrappers at every handler which can potentially throw an exception?
Preferrably I'm interested in WinForms. I'm not sure whether differences between WinForms and WPF exist in this field.
Aucun commentaire:
Enregistrer un commentaire