r/java • • Sep 20 '26

Stacktraces have such a weird ordering to them

Let's assume that my main() method represents index 1, and the actual failing method that is throwing the exception represents index 10.

Why on earth are exception stack traces printed like this?!

message for 4
4
3
2
1
message for 7
7
6
5
4
message for 10
10
9
8
7

Ignoring the fact that I have to mentally rearrange the stack trace elements into the right order, the actual error I care about is all the way at the bottom!

Why don't you just do it like this instead?

message for 10
10
9
8
message for 7
7
6
5
message for 4
4
3
2
1
  • The cause of this entire stack trace is at the top, where it should be.
  • The stack trace elements unravel very intuitively -- from the source of the problem down to the original caller.
  • No more redundant entries of 4 and 7.

    • Look at the original stack trace -- there are 2 entries of 4 and 7, presumably to help you piece the jigsaw-puzzle that is your stack trace back together again lol.
22 Upvotes

31 comments sorted by

View all comments

Show parent comment

2

u/davidalayachew Sep 20 '26

When using Intellij if you're pasting a trace from a logging app or something, you can go to code -> analyze stack trace and paste it in there. It'll show your project classes in blue (as you typically see in the console when running an app). Seeing the blue lines makes it easy to ingest and easy to ignore framework/lib lines from the trace, as you can quickly click through to the lines.

While that would certainly help, a lot of times, the stack trace is 75% my code. I truly am jumping that many levels deep sometimes.

3

u/aoeudhtns Sep 21 '26

You're stumbling on a corner of why the whole checked vs unchecked debate exists.

Things you see out there in the wild:

  • methods everywhere with throws Exception
  • using unchecked exceptions instead
  • custom exception type hierarchy (e.g. <Framework>Exception as the only type declared on a method, and then you have to inspect the subtype at runtime, or pray that the javadoc is accurate)

And then the problems you see:

  • Cycles in the linked exceptions
  • Re-throw logic fails to link original exception, i.e. swallows the cause
  • Caused-by vs suppressed, and lack of support in many tools for the latter

the stack trace is 75% my code. I truly am jumping that many levels deep

Can't say much confidently without specifics, but I have a gut feeling that wrapping and rethrowing within layers under your own control, smells like there's something off there. You're designing error cases that the layer above can't handle. One possibility is that the layer above should gain the throws for that exception type since it knows it can encounter it but can't handle it. Many people solve this with unchecked exceptions (but I know that is controversial and there are many advocates that unchecked should be reserved for "programmer error" type of problems).

And one thing I often ask on my team, is it really an exception or just a known error state. An example of that would be an HTTP protocol client throwing an exception for a non-2xx response, vs. handing back a Response structure with the headers, code, and body regardless of what those contents might be.

Or, a tale of two entity lookups:

 Optional<Foo> findFoo(ID id);
 Foo findFoo(ID id) throws NotFoundException;

I would argue the Optional is always better in the lower-layer code, because whether the entity existing or not being exceptional depends on the semantics of the situation. A caller could always throw if the Optional is empty. Some data structures in Java have multiple lookup paths, like Queue element / peek that differ only in whether a value is returned or exception thrown.

3

u/davidalayachew Sep 21 '26

Can't say much confidently without specifics, but I have a gut feeling that wrapping and rethrowing within layers under your own control, smells like there's something off there.

Yes, that is also true. A classic case of trying to over-generalize. It wasn't clear at the time that that would be the cause of part of my exception pain.

Or, a tale of two entity lookups:

Yes, this goes back to the whole exceptional vs expected error cases discussion.

I too think that Optional would be better there.

3

u/aoeudhtns Sep 21 '26 edited Sep 21 '26

I understand completely. I speak from experience of having done things like that myself!