How inefficient is it to get the stack trace for an exception? I know it's costly, but how costly? Should they definitely not be used in production environment?
5 Answers
In a production environment, it's helpful to log the stack trace so the user can find it when they contact tech support. Printing stack trace in place of an understandable (by the typical user) message should be avoided.
You shouldn't be concerned with the efficiency of exceptional blocks of code. Error recovery is the most important thing here.

- 398,270
- 210
- 566
- 880
-
5In addition to this, sharing a stack trace on a public facing site, COULD pose a security risk. – Mitchel Sellers Oct 21 '08 at 20:37
If exceptions are on your critical path, you've already got performance issues. Getting the stack trace to track down the exception is crucial, IMO.

- 84,552
- 17
- 108
- 152
My question is why are you caring about performance when something has unexpectedly gone wrong? At this point the sanity of your application is in question so who cares if it's fast?

- 25,225
- 10
- 61
- 100
-
2So, as you say, these are questions not answers. That would make a good comment on the question itself but definitely not an answer. – mbx Jul 22 '11 at 12:54
-
1@mbx, rhetorical "questions" can indeed be answers. You should not get tricked by syntax into confusing content. Suggesting that caring about the performance of exceptional situations is silly is spot on. – Kirk Woll Jan 02 '13 at 19:38
-
@KirkWoll Some rhetorical questions can indeed be part of an answer but a comprehensive answer deserves some more explanation. – mbx Jan 07 '13 at 12:21
Bills point is really spot on the money, and I added a comment, but wanted to add an answer here as well.
In a production environment, in a public facing site, I would NEVER print the stack trace to the user. Depending on the nature of the errors encountered the stack trace can containe information that can leak secure information (database names, etc.)
This same rule goes for error messages.

- 62,228
- 14
- 110
- 173
I usually only print or save the stack trace if I know that it can occur in a part of the system that is heavily dependent on other parts or other systems. This is especially true for pieces responsible for integration as the error might be intermittent and be heavily dependent on the state of the environment.

- 1,576
- 1
- 11
- 11