Sometimes it impossible to test for ALL possible exceptions. Sometimes you test on a device and it works, and another device gives a certain exception. In most cases, it is useful for the developer to receive error reports from users. This, of course, happens with the user’s will to cooperate by sending an email containing the exception details.
The simple code I put together is in VB (yes I am an antique-collecting dude who just loves VB), prepares an email for the user to be sent to your support email. Sometimes I am really frustrated how almost all WP7 development article give C# codes.
[crayon lang=”vb”]Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs) Handles Me.UnhandledException
Dim a As MessageBoxResult
a = MessageBox.Show(“An error has occured, if you like to send a report about that error to the developer to improve the app?”, “Oops”, MessageBoxButton.OKCancel)
If a = MessageBoxResult.Cancel Then GoTo Refuse ‘YES we can use Goto in VB
If a = MessageBoxResult.OK Then
Dim errormessage As String
errormessage = Date.Today.ToString + Key.Enter + e.ExceptionObject.Message.ToString + Key.Enter + e.ExceptionObject.StackTrace
Dim email As New EmailComposeTask
email.To = “support@YOURAPP.com”
email.Body = errormessage
email.Subject = “YOURAPPNAME Anonymous Error Report”
email.Show()
End If
Refuse:
If System.Diagnostics.Debugger.IsAttached Then
‘ An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break()
End If
End Sub[/crayon]
this code goes into “App.xaml.vb” to handle all unhandled exceptions by displaying a messagebox to the user asking him/her to send the details of the error to the developer.
You have to put this as the first line in your App.xaml.vb :
[crayon lang=”vb”]Imports Microsoft.Phone.Tasks[/crayon]
Afterwards, you’re good to go.