8

Can I throw some text in the stack trace?

My application already has a grip on the exceptions thrown. But would like to have some more information on some methods (parameters).

The idea was to do something like

StackTrace.Insert (0, "argument:" + test);

I know this is not cool, but is it possible?

5
  • Would this be sufficient? throw new InvalidOperationException("argument1:" + test + ", argument2:" + test2); Commented Sep 25, 2012 at 19:17
  • but it will stop my method, no? Commented Sep 25, 2012 at 19:18
  • 4
    Could the solution be to create the exception, and put the parameters in the Data dictionary property? (Then throw the exception...) Commented Sep 25, 2012 at 19:27
  • Do you have any logging infrastructure in your application? Commented Sep 25, 2012 at 19:36
  • 2
    Edited: There's nothing WPF specific here Commented Sep 25, 2012 at 19:51

3 Answers 3

13

Create the exception, and put the parameters in the Data dictionary property. Then throw the exception...

Sign up to request clarification or add additional context in comments.

2 Comments

Made it an answer since my comment was voted useful, and I think it's more of an answer than a comment.
Very nice. The key is to throw this newly created exception otherwise the StackTrace will not be populated.
5

Just to add to @erikH's excellent answer:

In addition to using the Data property, if you need additional data available up the exception chain, you can do so by means of a custom exception. In addition to the meta-information that a custom type can provide (a CommunicationException might mean a problem with I/O, an InvalidStateException might mean the program encountered an invalid state, and so forth) you can add additional information to the exception in the form of custom fields. Custom fields are especially useful when you need type safety that the Data property doesn't provide.

Comments

2

By definition, the stack trace is an output of the active stack frames at the current execution point in a program, generally accessed via an Exception.

@David Yaw gave you the correct way to add additional information to a stack trace, as it bubbles up the exception stack. To rephrase, you should only be adding your custom data to the stack trace, when it is the result of an exception.

Either create some centrally accessible List collection to store logging data in, or use proper try/catch with exception bubbling as David recommended.

for example.

try
{
  doSomethingThatMightFailAndThrowAnException();
}
catch (Exception ex)
{
  throw new Exception("Here is some custom data to add to the trace...",ex);
  // Notice adding the ex as the second parameter preserves the original exception trace
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.