![]() |
VOOZH | about |
Stacktrace in Node is a report that displays the error along with the path or sequence of execution. It provides the sequence of function calls or operations that led to an error in a program. It helps to debug the issues by providing the path of execution.
A Stack trace is displayed automatically by the JVM to indicate that an error has occurred during the program execution. The stack trace is used to trace the active stack frames at a particular instance during the execution of a program. The stack trace is useful while debugging code as it shows the exact point that has caused an error. Errors in Node.js can be classified into four broad categories:
Node.js supports several mechanisms for propagating and handling errors that occur during program execution. All Standard JavaScript Errors are handled immediately by throwing an error which can be viewed in the stack trace. There are four methods to print the stack trace in Node.js that are:
Table of Content
The error.stack property is a common way to print the stack trace. It automatically displays the stack trace when an error is thrown. It describes the point in the code at which the Error was instantiated.
error.stackExample: This example demonstrates generating and printing a stack trace using the Error().stack property.
Output:
This program demonstrates stack trace in Node.js
Error
at Object. (/home/cg/root/2523129/main.js:20:11)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3This method is used to create a .stack property on obj that returns a string representing the point in the code at which Error.captureStackTrace(obj) was called. The funcction parameter represents a function which is optional.
Error.captureStackTrace(obj, func)Example: This example demonstrates using Error.captureStackTrace() to programmatically create a stack trace.
Output:
Error
at Object. (/home/cg/root/2523129/main.js:25:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3It is a mechanism of Error Handling and it is used when a piece of code is surrounded in a try block and throw an error to the catch block. If the error is not handled then the program terminates abruptly.
Example: This example demonstrates how to throw an error using throw new Error() and catch it with a try-catch block, logging the error object and the stack trace to the console.
Output:
Error
at Object. (/home/cg/root/2523129/main.js:25:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3The console.trace() method is used to display the trace which represents how the code ended up at a certain point.
Example: This example uses console.trace() to print the current stack trace along with the message "hello world", showing the flow of execution up to the point where it's called.
Output:
Trace: hello world
at Object. (/home/cg/root/2523129/main.js:28:9)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3