Stack Trace
- In Turkish
- Stack trace
In short
A stack trace is the list of function calls in progress when an error happened, from the line that failed back to where the program started.
What is a stack trace?
When a program crashes or throws an exception, it usually prints a stack trace: the functions that were running at that moment, each with its file name and line number. It is a snapshot of the call stack, the record a program keeps of which function called which, so each one knows where to return when it finishes.
Most languages print the innermost call first: the line where the error was raised is at the top, followed by the function that called it, then that function's caller, down to the program's entry point. Python does it the other way round and prints its traceback with the most recent call last, just above the error message. Lines from your own code are usually the ones to read; frames from libraries and the runtime show how the call got there.
Reading a stack trace is like retracing your steps when you lose your keys: you start where you noticed they were missing and work back through each place you went. In production, stack traces go to logs and error trackers so developers can find bugs they can't reproduce. That is also why they should never be shown to users: file paths and function names can help an attacker.
Key takeaways
- A stack trace lists the function calls in progress when an error happened.
- Each line, or frame, names a function, a file and a line number.
- Most languages put the failing line first; Python puts it last.
- Stack traces belong in logs and error trackers, not in front of users.
Example
function parsePrice(text) {
return Number(text.trim());
}
function total(items) {
return items.reduce((sum, item) => sum + parsePrice(item.price), 0);
}
total([{ price: "4.50" }, {}]);
// TypeError: Cannot read properties of undefined (reading 'trim')
// at parsePrice (cart.js:2:22) ← where it failed
// at cart.js:6:44
// at Array.reduce (<anonymous>)
// at total (cart.js:6:16)
// at Object.<anonymous> (cart.js:9:1) ← where it startedReaders ask
What is the difference between a stack trace and a log message?
A log message is a line the program writes on purpose to say what it is doing. A stack trace is produced automatically when an error occurs and shows where in the code it happened. A good error log has both: a message saying what failed, and the stack trace showing where.
Why does a stack trace show code I didn't write?
Your code runs inside other code: frameworks, libraries and the language runtime call your functions and are called by them, and their frames show the path the call took. Start reading at the first frame from your own files; that is usually where the fix belongs.
See also
- ExceptionProgramming Fundamentals, p. 19An exception is an event that interrupts a program's normal flow when an error occurs, which code can catch and handle instead of letting the program crash.
- DebuggingProgramming Fundamentals, p. 15Debugging is the process of finding out why a program misbehaves, locating the faulty code and fixing it, often with the help of a tool called a debugger.
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- StackData Structures, p. 31A stack is a data structure that stores items in last in, first out (LIFO) order, so the most recently added item is always the first one removed.
- Stack MemoryOperating Systems, p. 28Stack memory is where a thread keeps its functions' local variables and return addresses, growing with each call and shrinking automatically on return.
- LoggingDevOps & Cloud, p. 35Logging is the practice of recording timestamped messages about events in a running program, such as errors and requests, so people can investigate them later.
Sources
Spotted a mistake or something missing on this page?Suggest an edit