Skip to main content

Stack Trace

In Turkish
Stack trace
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/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

A stack trace from a JavaScript error, read from the topjavascript
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 started

Readers 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

Sources

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings