How to get and print the current stack trace in Java?
Hello, colleagues! I recently started programming in Java and encountered a situation: in my multithreaded application, some strange bugs occasionally appear that are hard to catch. I want to simply see at a certain point in the code where the execution thread came from — that is, get the current call stack to understand the sequence of methods that led to this point.
I know that when an exception occurs, we can do `e.printStackTrace()`, but what if there is no exception and I just need to log the current stack for debugging? For example, I want to add such diagnostics in the `processOrder()` method to understand who is calling it:
```java
public class OrderService {
public void processOrder(int orderId) {
// Here I want to get the stack and log it
System.o
No answers yet.
Log in to leave an answer