<p>What's the easiest way to print a stacktrace from a debugging printout? Often during testing you would like to know the callstack leading up to the situation provoking a debug message.</p>

[{'answer_id': 54887, 'author': 'jjnguy', 'author_id': 2598, 'author_profile': 'https://Stackoverflow.com/users/2598', 'pm_score': 2, 'selected': False, 'text': '<p>You should be catching the exception in a try-catch block.</p>\n\n<pre><code>e.getStackTrace();\n</code></pre>\n\n<p>That returns StackTraceElement[] that you can then interpret.</p>\n\n<p>Also:</p>\n\n<pre><code>e.printStackTrace()\n</code></pre>\n\n<p>will...print the stacktrace.</p>\n'}, {'answer_id': 54892, 'author': 'Johan Lübcke', 'author_id': 4220, 'author_profile': 'https://Stackoverflow.com/users/4220', 'pm_score': 2, 'selected': False, 'text': '<p>Just creating an arbitrary exception does the trick for me:</p>\n\n<pre><code>System.out.println("Oops, the bad thing happened");\nnew IllegalStateException().printStackTrace();\n</code></pre>\n'}, {'answer_id': 54896, 'author': 'Paul Tomblin', 'author_id': 3333, 'author_profile': 'https://Stackoverflow.com/users/3333', 'pm_score': 2, 'selected': False, 'text': "<p>As well as what @jjnguy said, if you don't have an exception, you can also call Thread.getStackTrace().</p>\n"}, {'answer_id': 54944, 'author': 'Dan Dyer', 'author_id': 5171, 'author_profile': 'https://Stackoverflow.com/users/5171', 'pm_score': 5, 'selected': True, 'text': '<p><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#dumpStack()" rel="noreferrer">Thread.dumpStack();</a></p>\n'}, {'answer_id': 56284, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>If you want to save the stack trace into a String you can do this;</p>\n\n<pre><code>String exception = "";\nfor (StackTraceElement element : e.getStackTrace())\n   exception += element.toString() + "\\n";\n</code></pre>\n\n<p>Where e is, obviously, an exception.</p>\n\n<p>Besides, it sounds very weird to autogenerate an own Exception just to find get a stack trace for a debug. Get Eclipse and use it\'s debug mode, it\'s really awesome.</p>\n'}, {'answer_id': 59775, 'author': 'shsteimer', 'author_id': 292, 'author_profile': 'https://Stackoverflow.com/users/292', 'pm_score': 4, 'selected': False, 'text': '<p>If you\'re using log4j</p>\n\n<pre><code>Exception e = new Exception();\nlog.error("error here", e);\n</code></pre>\n\n<p>will print the stacktrace to your log.</p>\n'}, {'answer_id': 1742636, 'author': 'sleske', 'author_id': 43681, 'author_profile': 'https://Stackoverflow.com/users/43681', 'pm_score': 1, 'selected': False, 'text': '<p>Just because I needed it myself:</p>\n\n<p>As inspired by answer <a href="https://stackoverflow.com/questions/421280/in-java-how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection">How do I find the caller of a method using stacktrace or reflection?</a> , you can retrieve the call stack using </p>\n\n<pre><code>StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace()\n</code></pre>\n\n<p>Then you process and print/log whatever you are interested in. More work than using <code>Thread.dumpStack()</code>, but more flexible.</p>\n'}, {'answer_id': 36439329, 'author': 'minipif', 'author_id': 1440076, 'author_profile': 'https://Stackoverflow.com/users/1440076', 'pm_score': 2, 'selected': False, 'text': '<p>To simply print the current stack trace to stderr, you can call:</p>\n\n<pre><code>Thread.dumpStack();\n</code></pre>\n\n<p>which itself just calls:</p>\n\n<pre><code>new Exception("Stack trace").printStackTrace();\n</code></pre>\n\n<p>To output to <strong>stdout</strong> rather than stderr, pass <code>System.out</code> to <code>printStackTrace()</code>:</p>\n\n<pre><code>new Exception("Stack trace").printStackTrace(System.out);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/54882', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4220/']