views:

619

answers:

5

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?

A: 

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.

IAmCodeMonkey
+10  A: 

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.

Bill the Lizard
In addition to this, sharing a stack trace on a public facing site, COULD pose a security risk.
Mitchel Sellers
+5  A: 

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.

Mark Brackett
+4  A: 

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?

Quibblesome
+1  A: 

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.

Mitchel Sellers