CodeGym/Java Course/New Java Syntax/Practice displaying the stack trace

Practice displaying the stack trace

Available

"Hi, Amigo!"

"Hi, Diego!"

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

15
Task
New Java Syntax,  level 15lesson 2
Locked
Returning a stack trace
There are five methods that call each other. You need to make each method return its stack trace.
15
Task
New Java Syntax,  level 15lesson 2
Locked
Stack trace revisited
There are five methods that call each other. You need to make each method return the name of the method that called it. Use the stack trace to obtain this information.
15
Task
New Java Syntax,  level 15lesson 2
Locked
Who called me?
There are five methods that call each other. You need the method to return the line number of the code from which it was called. Use the element.getLineNumber() method.
15
Task
New Java Syntax,  level 15lesson 2
Locked
Logging stack traces
Implement the log method so that it displays the name of the file and method in which it is called, as well as the passed message. Use a colon and space to separate the file name, method name, and message. Example output: Solution.java: main: In method Getting the file name from a StackTraceElement
15
Task
New Java Syntax,  level 15lesson 2
Locked
Stack trace with 10 calls
Write the code to get a stack that is 10 calls deep. Don't change the main method.
15
Task
New Java Syntax,  level 15lesson 2
Locked
The method returns a result — the depth of its stack trace
Implement a method that returns the stack trace depth, i.e. the number of methods in the stack trace. In the method, the same number needs to be displayed on the screen.
Comments (55)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Japa
Level 14
19 May 2023, 17:41
if lessons only teach you use foreach, use for(i) iterator to check value what is needed.
Melody Ye
Level 29 , France, France
6 March 2023, 15:35
For anyone one's confused about these tasks, there are some info might be useful: "return Thread.currentThread().getStackTrace()[2].getMethodName();}" This code is a method that returns the name of the method that called the current method, using the stack trace of the currently executing thread in Java. Let's break it down: Thread.currentThread() returns a reference to the currently executing thread. getStackTrace() returns an array of StackTraceElement objects representing the method call stack for that thread. [2] is used to access the third element of the stack trace array (the element at index 0 is the method that called getStackTrace(), the element at index 1 is the getStackTrace() method itself, and the element at index 2 is the method that called the method that called getStackTrace()). getMethodName() is called on the StackTraceElement object at index 2 to retrieve the name of the method that called the current method. So in summary, return Thread.currentThread().getStackTrace()[2].getMethodName(); returns the name of the method that called the current method, using the stack trace of the currently executing thread.
Ndatta Fall
Level 25 , United States of America, United States
29 March 2023, 15:59
this was so helpful! God bless you!
Daniel Ketcheson
Level 28 , Canada
25 May 2023, 17:59
Daunting at first, this lesson became kind of easy once I read your explanation. Another thank you!
Xavi Martin
Level 22
Expert
5 December 2022, 10:34
The worst lesson of all, I understand that as a programmer you have to wake up but could you explain something? It is supposed that the course is to learn and if they put you some exercises that you do not even know where to start looking for when it would be much more useful to explain it well and instead of wasting time searching without success, practice more, I say...
LX40GL
Level 28 , Birmingham, United States
8 September 2022, 14:07
I'm using Google. And I downloaded Java API document at Level 09. I don't ask CodeGym to explain everything. But at least give us some basics or keywords. This lesson is confusing.
Jonaskinny Java Developer at Sandmedia
20 February 2022, 10:27
This one got me initially since I was not counting the getStackTrack() in the method call chain. Good reminder !!
PeterC Android Developer
6 December 2021, 22:17
Don't use COPY - PASTE. Type it all manually - this is the best way to develop muscle memory.
Ricky
Level 21 , Norway
25 January 2022, 02:15
Although unbelievably tedious, this is unfortunately true. Good tip
Justin Smith
Level 41 , Greenfield, USA, United States
21 July 2021, 13:08
Other than debugging, what would be the actual use of the getLineNumber method?
Ashraf Khalifa
Level 9 , Coventry, United Kingdom
20 June 2021, 15:30
Felt like I was thrown in the deep end. Not enough explanation on the subject.
John Squirrels Website Admin at CodeGym
22 June 2021, 13:52
If you have got some questions, please ask us.
Anonymous #10775689
Level 14 , United Kingdom
2 August 2021, 08:25
I think the comments/replies here show that an extra lesson with explanation would really help learn this topic.
Michael
Level 10 , Dresden, Germany
12 August 2021, 06:25
Instead of wasting so much time looking around independently, I do prefer to be better prepared for the challenges. Concerning asking questions: The Codegym team and community are very active and responsive, but sometimes it takes time to get an answer. Quite simply, codegym is not a personal tutoring platform where the direct nature of the interaction is prominent. Many of us simply do need more explanations.
ImDevin
Level 15 , Old Town, United States
6 May 2021, 15:12
Well, I learned something about StackTrace, but this was a very strange set of tasks.
18 April 2021, 17:44
A detailed tutorial or, at a minimum, a table listing all the methods and their use would be preferred and could then be used to apply the knowledge to the exercises for practicing the usage and application. I did my best to complete each on my own and when unsuccessful, I would compare to the solution, copy and paste it in, and then analyze the resulting completed program.