0

The other day I needed (admittedly for the first time) to get a stack trace as a string. e.printStackTrace() of course doesn't return one.

I needed to use the solution in this answer to get what I wanted.

Yes I realise you can get the stack trace elements then iterate over them yourself, and there are 3rd party libraries that can do it in one line, but why is there nothing in the standard library which does this for you and returns a string?

noobcoder
  • 6,089
  • 2
  • 18
  • 34
  • Four lines of code? Write a helper method. – Thorbjørn Ravn Andersen May 31 '17 at 20:51
  • It sounds a little petty, but then again why write 4 lines of code when you can write 1 line with a clean method call? I assume that's the reason it was included in many commonly used 3rd party libs – noobcoder May 31 '17 at 20:54
  • It's annoying but that is the way it is. If you want to know _why_ you have to ask the architects at Oracle. – Thorbjørn Ravn Andersen May 31 '17 at 21:27
  • 2
    Well, maybe Sun/Oracle did not have the time to implement all the methods everyone needs. Otherwise we would not need any 3rd party lib at all, or, "why do they not implement the `doWhatCarlosNeeds` or `doWhatDonaldNeeds` method so we only have to write one line of code?" – user85421 May 31 '17 at 21:29
  • "It sounds a little petty", but if three lines of code are too much extra, you could write your own utility method. Personally I question the value of collapsing a stack trace into a `String`. Whether for human or automated consumption, a list-like structure is easier to parse. – Lew Bloch May 31 '17 at 21:34
  • @ThorbjørnRavnAndersen - FCS, can we spare ourselves from these *"yadda yadda you have to ask underspecified and unreachable-person at multi-million dollar company"* comments. Bah. What you *do* is, you ask on S.O. and hope that one of the actual experts is reading it. And while they're not reading it, *other* experts may come up with some good wisdom. (Or the mob community might close your question, *shrug*.) – Martin Ba Jun 01 '17 at 11:18
  • @MartinBa Ah, a meta-comment. Allow me to introduce you to https://meta.stackoverflow.com/ where they belong. You can read the introduction here: https://stackoverflow.com/help/whats-meta – Thorbjørn Ravn Andersen Jun 01 '17 at 15:36
  • @ThorbjørnRavnAndersen You may think they belong on the meta graveyard. I comment to reach a specific audience. – Martin Ba Jun 01 '17 at 15:40
  • @MartinBa Well, you can say so in a much less... shall we call it "colloquial way" to be polite. You might even provide an actual answer to what is being asked while we wait for _other_ experts to say they don't know why either or the question to be closed. – Thorbjørn Ravn Andersen Jun 01 '17 at 15:52
  • Do you expect the JRE to provide *everything* you might ever need as convenience method, even if you are probably the only one on the planet needing this? – Holger Jun 09 '17 at 11:24

1 Answers1

1

I always use e.toString() it's quite enough. If you want something more specific you can process the stack trace String result = ex.toString() + "\n"; StackTraceElement[] trace = ex.getStackTrace(); for (int i=0;i<trace.length;i++) { result += trace[i].toString() + "\n";} return result;

dade.sliep
  • 137
  • 1
  • 5
  • Sorry, i really don't know why Java creators didn't implemented an easier way to convert stacktrace element to string. I just can suppose that they considered that it wasn't so necessary for exception handling. – dade.sliep May 31 '17 at 21:34
  • They fit all that code on one line, just like the question asked. Why would you question whether they read the question? – Lew Bloch May 31 '17 at 21:35
  • @LewBloch because I didn't ask how to get the stack trace as a string. Besides I already stated I knew about getting the stack trace elements, but he provided a solution for that too. – noobcoder May 31 '17 at 22:04
  • But you did say, "The other day I needed ... to get a stack trace as a string" and "... do it in one line, but why is there nothing in the standard library which does this for you and returns a string?" So, yes, you did ask how to get the stack trace as a string in one line, and this answer shows you a way to do that. Clearly they _did_ read your question so the snarkiness wasn't necessary. – Lew Bloch Jun 01 '17 at 14:56