Setting Exception._stackTrace value by reflexion causes a 'FatalExecutionEngineError'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arbiorix
    New Member
    • Aug 2008
    • 5

    Setting Exception._stackTrace value by reflexion causes a 'FatalExecutionEngineError'

    I need to set the value of the StackTrace property of an exception.

    As this property has not setter, I do it by reflection on the private field _stackTrace.

    When accessing the exception object, I get a FatalExecutionE ngineError.
    Any idea why?

    Thank you.


    Exception e = new Exception();
    FieldInfo stackTraceField Info = typeof(Exceptio n).GetField("_s tackTrace", BindingFlags.In stance | BindingFlags.No nPublic);
    stackTraceField Info.SetValue(e , serviceExceptio nData.StackTrac e);
    ...
    Console.WriteLi ne(e)



    FatalExecutionE ngineError was detected
    Message: The runtime has encountered a fatal error. The address of the error was at 0x79e7b217, on thread 0x14bc. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You know you can just type cast any derrived exception type into its base Exception type?
    [code=c#]
    Exception e = (Exception)serv iceExceptionDat a;
    Console.WriteLi ne(e)
    [/code]

    Comment

    • Arbiorix
      New Member
      • Aug 2008
      • 5

      #3
      Originally posted by Plater
      You know you can just type cast any derrived exception type into its base Exception type?
      [code=c#]
      Exception e = (Exception)serv iceExceptionDat a;
      Console.WriteLi ne(e)
      [/code]
      Would be easy..., but serviceExceptio nData is just the object holding the StackTrace string I need to inject in the new exception. In my case, ServiceExceptio nData does not derivate from Exception.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        And the strack trace in that string does not match the stracktrace that would exist if you went "throw new Exception()" at the same time as that string was populated?

        Comment

        • Arbiorix
          New Member
          • Aug 2008
          • 5

          #5
          Originally posted by Plater
          And the strack trace in that string does not match the stracktrace that would exist if you went "throw new Exception()" at the same time as that string was populated?
          I don't think this is a matter of 'Matching' or not.
          When I do "Exception e = new Exception()", the StackTrace value is null.
          Then, I set it to a valid StackTrace string. For example,

          stackTraceField Info.SetValue(e , Environment.Sta ckTrace);

          would produce the same problem.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Hmm, I guess the stacktrace value doesn't get populated until you "throw" it.
            I am not sure why you cannot just set the value, possibly due to the way a debugger would interpret it, since it would use the stack trace to decide debugging information?
            Was there anything on the net about the error message you got?

            Comment

            • Arbiorix
              New Member
              • Aug 2008
              • 5

              #7
              Originally posted by Plater
              ...the way a debugger would interpret it, since it would use the stack trace to decide debugging information?
              If I enclose the above code in a try-catch, and run it in non-debbuging mode, no exception is catched. The app simply crashes with the classical "SetExceptionSt ackTrace has encountered a problem and needs to close. We are sorry for the inconvenience. If you were in the middle of something ...."

              Originally posted by Plater
              Was there anything on the net about the error message you got?
              I didn't find any relevant information. That's why I posted this as a question here.

              But you remarks gave an idea: I will study what MSDN says about 'Diagnosing Errors with Managed Debugging Assistants'. If someone has already some experience with this kind of stuff, please don't hesitate to give me your comments!

              Comment

              • Arbiorix
                New Member
                • Aug 2008
                • 5

                #8
                Ok, I got it.

                The backing field of Exception.Stack Trace is not _stackTrace but _stackTraceStri ng.

                Thanks Plater for your support.

                Comment

                Working...