简体   繁体   中英

how to catch an unhandled exception in windows phone 7 application?

how to catch an unhandled exception? if all my code wrapped in a try catch ? but an exception occurs all the same, and the application crashes ... Maybe there is some general advice?

use:

        try
            {
             ...my code
            }
        catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(),"Error!",MessageBoxButton.OK);
            }

Sample code is not present, because there is a big code and done many things ... and it looks like somewhere an error occurs, but where it is try-catch does not show, and the application just closes ...

add:

var errorw = MessageBox.Show(e.ExceptionObject.ToString(), "error", MessageBoxButton.OK); e.Handled = true; 

And the message: The parameter is incorrect.

Just as we now understand where and what parameter has given is not true? By the way forgot to write that the error occurs when you press Back, when you return to the previous page of the application.

would you try like this

      // Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender,ApplicationUnhandledExceptionEventArgs e)
{
    if (e.ExceptionObject is QuitException)
        return;

    if (System.Diagnostics.Debugger.IsAttached)
    {
        // An unhandled exception has occurred; break into the debugger
        System.Diagnostics.Debugger.Break();
    }

    //MessageBox.Show(e.ExceptionObject.ToString(), "Unexpected error", MessageBoxButton.OK);

    var errorWin = new ErrorWindow(e.ExceptionObject, "An unexpected error has occurred. Please click Send to report the error details.")
                       {Title = "Unexpected Error"};
    errorWin.Show();
    //((PhoneApplicationFrame) RootVisual).Source = new Uri("/Views/ErrorWindow.xaml", UriKind.Relative);

    e.Handled = true;
}

private class QuitException : Exception { }

public static void Quit()
{
    throw new QuitException();
}

There is always a manual.

UnhandledException event description.

Have you tried the StackTrace property of Exception . It shows you where the exception has been thrown.

catch (Exception ex)
{
    MessageBox.Show(ex.StackTrace,"Error!",MessageBoxButton.OK);
}

Make sure that you build application in debug, not release mode. Than, go to VS Debug-Exceptions menu and check all 'Thrown' column to enabled. After that, launch application with attached debugger. Also, you can execute your code step-by-step.

Hey guys you could use the BugSense library to catch the data and then collect it! PS. I am one of the founders

Put your code in Try-Catch Block. I was also facing such problem, but then handled by Exception Handling Method.

try
 {

   // your code

 }

catch (Exception ex)
 {

   throw (ex);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM