1

Using Assembly CrittercismWP8SDK.dll, v2.0.0.0

In Old code below, Crittercism throws an NullReferenceException: at

[System.NullReferenceException] = {System.NullReferenceException: Object reference not set to an instance of an object. at CrittercismSDK.DataContracts.ExceptionObject..ctor(String exceptionName, String exceptionReason, String stacktrace) at CrittercismSDK.Crittercism.LogHandledExce...

StackTrace = " at CrittercismSDK.DataContracts.ExceptionObject..ctor(String exceptionName, String exceptionReason, String stacktrace)\r\n
at CrittercismSDK.Crittercism.LogHandledException(Exception e)\r\n

Old code

Exception exception = new Exception(description);  
exception.Data.Add(MethodName, methodName); 
Crittercism.LogHandledException(exception);  //NullReferenceException

New code, no exception:

try
{
    Exception ex = new Exception(description);
    ex.Data.Add(MethodName, methodName);
    throw ex;
}
catch (Exception e)
{
    Crittercism.LogHandledException(e);  //No NullReferenceException 
}

My theory is that the system populates the Exception object in a way I cannot or that I have missed. Any ideas why the Old code causes Crittercism to throw a NullReferenceException?

Community
  • 1
  • 1
ezaspi
  • 684
  • 7
  • 25

2 Answers2

1

If you don't throw an Exception, just new it up, the stacktrace is null

Lorentz Vedeler
  • 5,101
  • 2
  • 29
  • 40
1

Creating the Exception this way

Exception exception = new Exception(description);  
exception.Data.Add(MethodName, methodName); 
Crittercism.LogHandledException(exception); 

and passing it directly to Crittercism results in that the StackTrace-Property of the exception is set to null. Which I think is the problem for Crittercism in this case.

In the catch block the StackTrace-property initialize so that Crittercism will not throw a NullReferenceException in this case.

Jehof
  • 34,674
  • 10
  • 123
  • 155
  • Thanks gents, I've filed a bug for Crittercism. We'll confirm this and put out a fix. It's true that the more accurate way of sending an exception is certainly throwing it from within a try/catch block as you get the stack trace included. – pixelknitter Jun 26 '14 at 19:10
  • A couple things: 1. Both the answers are the same. Can I accept both or only one? I assume the first to get there is the one I accept? 2. I will accept tomorrow to give time for confirmation. There's no way I can tell absolutely that this is the case. But it sounds right. – ezaspi Jun 26 '14 at 21:01
  • @ezaspi you can only accept one answer and it is up to you which answer provides you the best information and answers you question. – Jehof Jun 27 '14 at 05:34
  • Well Lorentz was first (by a minute or slightly more) but this answer is a bit more informative in that it mentions the catch block. – ezaspi Jun 28 '14 at 09:49