Probe the Secrets of the Stack Trace
Learn how to find useful clues in the stack trace to help you cure programming headaches.
October 30, 2009
Troubleshooting Tricks
LANGUAGES: VB .NET
TECHNOLOGIES:Stack Trace | Exceptions
Probe the Secrets of the Stack Trace
Learn how to find useful clues in the stack trace tohelp you cure programming headaches.
By Don Kiely
Frequently I'm asked for help with resolving exceptionmessages generated by ASP.NET applications, and I encounter many more suchrequests on the http://www.asp.netforums. All too often, the question is of the form, "Help! I'm getting thiserror: 'Object reference not set to an instance of an object.' How do I solveit??!? I'm on a tight deadline!" Of course, my only response can be that it'simpossible to be of any help without more information. Then I ask for twothings: the code of the procedure where the exception occurred plus any otherrelevant code (hoping my inbox isn't soon plugged with 10 megs of "relevant"code) and the exception message's complete text (see Figure 1).
Sometimes I must go back and forth with the person severaltimes because they don't believe I really want the entire text of the message,including all the nonsense junk at the bottom that has cryptic object names,strange numbers, and method names that aren't in the custom source code. Yes,all that blather at the bottom of the page is valuable. It's called a stack trace, and it provides valuableclues about how to resolve particular problems.
Figure 1 shows an example ofan exception message we're all painfully familiar with.
Figure 1. This is a typical errormessage. All the information on this page is vital - including the stuff at thebottom, which is called the stack trace.
I generated this simply by including this line of codedeep within a nested procedure on an .aspx page:
Throw New NullReferenceException
Simple enough. In this contrived example, the errorprobably is easy enough to find and diagnose. The exception message providesthe offending code, and usually you simply can look at that line, or maybe acouple before it, to figure out what's wrong. But that's really only the casewhen the exception is raised by your code.
Things get trickier when the exception is raised by codedeep within the .NET Framework that executes in response to something in yourcode. But your code calls a framework method, which calls another frameworkmethod, which calls another method, when ... zap! - that last method raises anexception. In this case your procedure is far below the top of the stack trace.That's a bug in the framework, right? Maybe, but it's more likely because you eithercalled the first framework method with parameters that Microsoft didn'tanticipate - don't underestimate an ASP.NET programmer's creative abilities -or some environmental condition didn't meet the needs of the deeply nestedmethod.
In cases like this you can look at your own code all daylong and never figure out what's up. But by carefully examining the stack traceyou can pick up many clues about what is happening. Examine the .NETdocumentation for the objects the framework is using as it moves deeper intothe framework. What kind of data do these nested objects expect that perhapsthey aren't getting? Are there inconsistencies in your parameters that arecausing confusion in the framework? I've found this can provide just enoughinformation for me to figure out the problem. And if all else fails, you canrun ildasm on framework DLLs and really get into the messy details of what'sgoing on. Thankfully, it's rare that I need to probe that deeply.
Incidentally, as you explore the stack trace for yourcustom procedures, be aware that the line numbers are actual lines in the codemodule's text, as though all regions are expanded. If you go to the file andcan't find the line number for any of the items in the stack trace, you mighthave an unexpanded region.
Next time I'll explore a way you can generate a stacktrace anytime in your own code - even when it hasn't thrown an exception.
The sample code in thisarticle is available for download.
Don Kiely is senior technology consultant forInformation Insights, a business and technology consultancy in Fairbanks,Alaska. E-mail him at mailto:[email protected].
About the Author
You May Also Like