undefined

Practice displaying the stack trace

Java Syntax
Level 9 , Lesson 2
Available

"Hi, Amigo!"

"Hi, Diego!"

"Here’s a small task about displaying the stack trace."

undefined
3
Task
Java Syntax, level 9, lesson 2
Locked
Returning a stack trace
The way a stack works is unfair and harsh. But sometimes it's so useful! Remember that the last function called is the first function to end. Let's practice and see what happens: we need to write five methods that call each other. Each method must return its stack trace.
undefined
6
Task
Java Syntax, level 9, lesson 2
Locked
Stack trace revisited
Let's get right down to business: write 5 methods that call each other. Each method should return the name of the method that called it. Use the stack trace to obtain this information. This name seems so beyond our reach. But we'll figure it out. These are just methods that sequentially call each other, nothing more.
undefined
3
Task
Java Syntax, level 9, lesson 2
Locked
Who called me?
The call stack may not be the most beautiful construct... But why not? It makes sense—you just need to get used to it. Let's get used to tasks! This time, you need to write five methods that call each other. Each method should return the line number of the code from which it was called. To do this, use the element.getLineNumber() method.
undefined
3
Task
Java Syntax, level 9, lesson 2
Locked
Stack trace with 10 calls
I got lucky: My favorite elementary school, Mrs. Turing, always wrote this phrase on the blackboard: "The last function called is the first function to end". She made us memorize it and swore that one day we would understand. Thank you, Mrs. Turing. It seems the promised day has come. I can even write code to get a stack trace that is 10 calls deep. Can you?
undefined
3
Task
Java Syntax, level 9, lesson 2
Locked
In the blue depths of the stack trace…
Remember the golden rule: function A calls function B, which calls function C. And to exit A, you must first exit B, and to do that you must exit C. Got it? Good. And now we'll write a method that returns the stack trace depth, i.e. the number of methods in the stack trace. And one more thing: display this number on the screen.
undefined
3
Task
Java Syntax, level 9, lesson 2
Locked
Logging stack traces
If you're approaching something more or less seriously, you will eventually come to some form of logging. Don't hesitate. Let's implement the log method. The log method should display the name of the class and method in which it is called, as well as the passed message. For clarity, use a colon and space to separate the class name, method name, and message.
Comments (39)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Russell Level  9, Galmpton, Brixham , United Kingdom
28 January 2021
Yeah im sorry but this lesson made me give up for months. In the end I had to do the excercises by copy pasting. They are not inas intuitive as previous exercises. Also I was not taught this :

return Thread.currentThread().getStackTrace()[2].getMethodName();
this line of code is not self-explanatory for me. Stack overload results from google are snippy and not always plesant to read. I couldn't think how to present the stack traces in the following excercises because the explanation of why i was calling element 2 specifically of the stacktrace function is not explained. I have come back now and to my horror have had to C/P a lot of answers and then try to work through the code given to figure out what the goddamn hell is going on. This lesson is truly poor show. Lets hope the rest of the lessons are more helpful and don;t simply rely on you googling the problem and then trying to work your way through the quagmire of passive-aggressive stackoverflow posts to find the ultimate answer presented in a way thats understandable for beginners. Poor Poor Show Codegym And Yes I do pay a subscription to codegym. I am not a freelaoder.
Guanting Liu Level  13, Springfield, United States
20 November 2020
If u consider stacktrace as a stack,u are definitely wrong.It has no relation with stack interface and it is an exact array type which will store array[0] = stacktraceelement,and array[1] = main,then starts from method1.
Jonathan Level  12, Esslingen, Germany
21 October 2020
For everybody desperately searching for solutions: There is a new feature from Codegym which shows the perfect solution. You just have to press the violet button in the right top corner. no problem
xq fang Level  11, Pittsburgh, United States
8 October 2020
My [Ctrl + c] and [Ctrl + v] skills were fully developed by these exercises oh yeah🙃
tina Level  9, Singapore, Singapore
19 September 2020
how is it applicable to real life requirements? also yes, more explanation w.r.t to class, methods within the stack trace could have been more useful.
joe Level  23, London, United Kingdom
14 August 2020
A few examples wouldn't hurt.................
Oliver Heintz Level  17, Mustang, United States
2 August 2020
I understand nothing. These tasks specifically caused me to take a month long break from this course, and even though I managed to do the last three tasks on the page without needing to look up help, I still have very little idea what I am doing, I just learned enough to copy and paste. I hope to god that this isn't important, or that understanding will come with time.
Norbert Level  9, Krakow, Poland
20 July 2020
Read more about Stack Memory and Heap Space in Java. Google it. Also through for loop u can display what values StackTrace is keeping. Hope it is helpful.
Edddieg Level  15, Providence, United States
12 July 2020
so in real life ,, what would we need stack trace for ?
Gellert Varga Level  14, Szekesfehervar, Hungary
31 May 2020
Here you can find other needed methods of the StackTraceElement class and descriptions about them: https://it.inf.unideb.hu/java8docs/api/index.html?java/lang/StackTraceElement.html