Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading Stack Traces #87

Closed
wants to merge 3 commits into from
Closed

Reading Stack Traces #87

wants to merge 3 commits into from

Conversation

danthe1st
Copy link
Contributor

@danthe1st danthe1st commented Apr 26, 2024

Request issue

closes #53

Website section

Learn > Tutorials > Exceptions > Reading Stack Traces

Notes for reviewers/specific decisions that may be worth reconsidering

  • As the other articles on exceptions are not included in this repository, I had to guess parts of the metadata of this article. Specifically, I made the assumption that the id of articles about exceptions starts with lang.exceptions and are in a group called exceptions.
    • I put this article in a directory called 05_exceptions. I did not include a 00_exceptions.md file/index page for the exceptions page. Hence, it the exceptions article is not listed anywhere when rending the site using this repository (I can't copy the description of that series as that isn't under the UPL).
  • As stack traces use tabs, I did not change that in this article (these are rendered using 4 spaces).
  • It might also be a good idea to include a link to this Stack Overflow question. However, the More learning section only seems to support YouTube links so that would need to be included in the summary or similar. (Alternatively, it would be possible to modify the _more_learning partial)

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Apr 26, 2024
@danthe1st danthe1st changed the title initial version of stack trace article Reading Stack Traces Apr 26, 2024
@danthe1st danthe1st marked this pull request as ready for review April 27, 2024 08:23
@carimura
Copy link
Member

Thanks, looks clear and appropriate for the Exceptions section. Stand by while I get a deeper review!

<a id="summary">&nbsp;</a>
## Summary

Even if stack traces may look intiminating at first, they are just a lot of diagnostic information helping you to find what caused the exception. As this information is often helpful in finding what caused the issue, you should typically not just ignore exceptions by adding a `System.err.println("An error occured");` or similar. Instead, either wrap the exception in another exception so that information about the original exception is preserved in a `Caused By` section or (if you actually want to continue executing after the exception occured) print the stack trace using the [`printStackTrace`](javadoc:Throwable.printStackTrace()) method.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Dan,

There is a small typo here: intimidating instead of intiminating

---
id: lang.exceptions.stacktrace
title: Reading Stack Traces
slug: learn/exceptions/stacktrace
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this aims to be under exceptions portion, a slug_history element is needed.

- Suppressed exceptions {suppressed}
- Reading bigger stack traces {bigger}
- Summary {summary}
description: "This article explains what stack traces are as well as how they can be read."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, the description is hard to grasp. Maybe consider rephrasing that "as well" to "and"?

<a id="intro">&nbsp;</a>
## What is a stack trace?

When an exception occurs in a Java program, a stack trace is often printed to the console. These stack traces often look scary, especially with complex frameworks but actually provide a lot of useful information for solving the issue causing the exeption. Specifically, stack traces show what exception occured as well as where exactly that happened and how the Java program got to that point.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, using an emotion (look scary) in this technical context needs a justification. If I were to just find out about stack traces...why are they scary? (maybe the amount of content they have makes them scary)....

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this section names induces to me the idea that I will find out what is a stacktrace and this definition is not clear enough "stack traces show what exception occured as well as where exactly that happened and how the Java program got to that point."
For me, a stack trace is a sequence of method calls that led to an exception being thrown in a Java program. A stack trace includes the names of the methods involved, along with the line numbers in the source code where the method calls occurred.

<a id="simple">&nbsp;</a>
## A simple stack trace

Before coming to complex stack traces, we first investigate a stack trace originating from a very simple Java application.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is the first section containing right after the introduction and since complex stack traces were discussed before, I would suggest to omit the "Before coming to complex stack traces" and keep the actual introductory comment.


First of all, Java tells us that we got an exception in a thread called `main`. As we haven't used anything related to starting other threads, this is the only application thread in this example. In applications involving multiple threads, the thread name can be a useful piece of diagnostic information. This part may be omitted or look different depending on what printed the stack trace.

After the thread name, the full class name of the exception is shown. As we have thrown an `IllegalStateException` in our example, this is `java.lang.IllegalStateException`. If a message is associated with the exception, that message is included in the stack trace as well. In our case, we passed the message `This is for demonstration.` to the `IllegalStateException` constructor resulting in that text being part of the stack trace.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a message is associated with the exception, that message is included in the stack trace as well.

The above phrase implies an else case...so can there be a message in a stack trace not generated by an exception?


After the thread name, the full class name of the exception is shown. As we have thrown an `IllegalStateException` in our example, this is `java.lang.IllegalStateException`. If a message is associated with the exception, that message is included in the stack trace as well. In our case, we passed the message `This is for demonstration.` to the `IllegalStateException` constructor resulting in that text being part of the stack trace.

The lines following start with the word `at` and provide detailed information on where the exception occured and what method calls lead to the program getting to that. The first such line is `at com.example.someapplication.StackTraceDemo.someMethod(StackTraceDemo.java:9)`. This tells us that the exception occured in a method called `someMethod` in the class `StackTraceDemo` located in the package `com.example.someapplication`. It also informs us that the exception was thrown in line 9 of the file `StackTraceDemo.java`. With many IDEs, you can even click on the `StackTraceDemo.java:9` and the IDE automatically opens the relevant file and navigates to the line in question. If we take a look at that line, we can see that it is `throw new IllegalStateException("This is for demonstration");` which matches the stack trace. Similarly, the next line is `at com.example.someapplication.StackTraceDemo.main(StackTraceDemo.java:5)`. This line tells us that where `someMethod` was called. This happened in the `main` method of our `StackTraceDemo` class.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lines following start with the word at

Can you please tell me to what does start refer to?

<a id="printing">&nbsp;</a>
## Printing stack traces

Sometimes, it is necessary to print the stack trace to the console or some other location when catching an exception without re-throwing it. For doing that, we can make use of the [`printStackTrace`](javadoc:Throwable.printStackTrace()) method. Calling that method prints the stack trace of a given exception to `System.err`. We can adapt our previous program to do catch the exception and print the stack trace by ourselves.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes, it is necessary to print the stack trace

As a reader, I would expect an example on when should I print the stack trace.

<a id="cause">&nbsp;</a>
## The `Caused by` section

Sometimes, you might want to [throw an exception that was caused by a different exception](/learn/exceptions/throwing/#chained-exceptions).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to have a justification on why (as a developer) would I want to do that 😄 . Maybe mention some best practice.


Even if stack traces may look intiminating at first, they are just a lot of diagnostic information helping you to find what caused the exception. As this information is often helpful in finding what caused the issue, you should typically not just ignore exceptions by adding a `System.err.println("An error occured");` or similar. Instead, either wrap the exception in another exception so that information about the original exception is preserved in a `Caused By` section or (if you actually want to continue executing after the exception occured) print the stack trace using the [`printStackTrace`](javadoc:Throwable.printStackTrace()) method.

```java
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought: maybe you can consider refactoring this section to not end with a code section but with how looking at stack traces influence debugging.

@carimura
Copy link
Member

After some internal discussion @danthe1st we are going to hold off on accepting this PR. Thank you for the effort and I'll let you know if we get to a point where we can re-work it for inclusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Request] How to read stack traces
3 participants