Reading a stack trace

Reading a Java stack trace: exception type and message, frames from newest to oldest, file and line number, library frames to skip and the Caused by chain.

  • 23 cards
  • 5 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. Understand

A few cards from the deck

  • Stack trace: what is it?

    Show the answer

    The list printed when an exception is not caught: the calls in progress at the moment of the error, newest first, each with its file and line.

    It answers two questions: where was the error thrown (first at line) and by which path did the program get there (the following lines).

  • This trace comes from the fr.iut project. Which line of the project's own code should be opened first?

    ```text
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 2
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
    at java.base/java.util.Objects.checkIndex(Objects.java:361)
    at java.base/java.util.ArrayList.get(ArrayList.java:427)
    at fr.iut.Panier.total(Panier.java:16)
    at fr.iut.Panier.traiter(Panier.java:29)
    at fr.iut.Panier.main(Panier.java:25)
    `

    • Panier.java:16
    • ArrayList.java:427
    • Preconditions.java:64
    • Panier.java:25
    Show the answer

    Panier.java:16

    The java.base/… frames belong to the Java library, which reacted correctly to a bad call. The first fr.iut frame is Panier.total, line 16: that is where the faulty index was requested.

  • In a Java trace, the … line introduces the exception that triggered the one above it.

    Show the answer

    In a Java trace, the Caused by line introduces the exception that triggered the one above it.

    An exception can wrap another one: the code caught the original error then threw a new one, keeping it as its cause.

  • Stack frame: what is it?

    Show the answer

    One at … line of the trace: a method that is running, with the source file and the line it had reached.

    at fr.iut.Panier.total(Panier.java:16) reads: class fr.iut.Panier, method total, file Panier.java, line 16.

  • A trace starts with five frames from java.util and jdk.internal before the first frame of your project. Which approach is right?

    • Open the very first frame: the flaw is in the library code itself
    • Open the last frame, since only the starting point explains the error
    • Look for the latest Java version, since the exception comes from the library
    • Go to the first frame of your project: your call caused the error
    Show the answer

    Go to the first frame of your project: your call caused the error

    Library code is extremely well tested: when it throws an exception, it is almost always because it was handed a bad value. The first frame of your code is the last place where you are in control.