How to handle unknown exception such as NULL exception.

SmilingMoon 981 Reputation points
2020-10-16T21:46:43.607+00:00

I developed UWP windows app deployed to MS Store.
Customer sent me an error " System.NullReferenceException: Object reference not set to an instance of an object.".
But, I have no idea where the error occurred and fix the issue. Because there is NO way I can catch line number of source code that caused the exception.

Unless I put bookmark every single line of source code, I have no idea how to handle this kind of issue.
What is the best practice for UWP for unknown error handling for bug fix?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Richard Zhang-MSFT 6,936 Reputation points
    2020-10-19T05:56:07.887+00:00

    Hello,

    Welcome to Microsoft Q&A.

    In the process of creating applications, null references are a common exception. This requires developers to consider the situation where variables are null as fully as possible when writing code.

    If a customer reports a problem with a null reference, you can follow the steps below (Just some suggestions):

    1. Ask the customer about the steps to reproduce the problem, and try to reproduce it locally to locate the problem.
    2. Review the code after fixing the problem, or create a separate unit test module for testing.

    In UWP, you can also listen to the UnhandledException event in App.xaml.cs. For most runtime errors, it will eventually bubble up here. After obtaining the error information, you can perform local logging for subsequent repairs:

       public App()  
       {  
           this.InitializeComponent();  
           this.Suspending += OnSuspending;  
           this.UnhandledException += OnUnhandledException;  
       }  
         
       private void OnUnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)  
       {  
           e.Handled = true;  
           // handle with e.Exception  
       }  
    

    Thanks.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.