Skip to content

Commit 1879e29

Browse files
authored
BAEL-5608 - Get current stack trace java (eugenp#12533)
* Initial commit for Object copy in Java * review comments commit for Object copy in Java * Initial commit for parseInt vs valueOf java * Review comments commit for parseInt vs valueOf java * Modify readme * review comments * build failure * build failure retry * build failure retry remove parseInt(java.lang.String,int,int,int) * build failure add comment * change examples * review comments * review comments 2 * review comments 3 * Initial commit for get current stacktrace * Remove old files * Name updates * Jenkins error * changes to file name * Review comments
1 parent 9403406 commit 1879e29

4 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.exception.currentstacktrace;
2+
3+
public class DumpStackTraceDemo
4+
{
5+
public static void main(String[] args) {
6+
methodA();
7+
}
8+
9+
public static void methodA() {
10+
try {
11+
int num1 = 5/0; // java.lang.ArithmeticException: divide by zero
12+
}
13+
catch (Exception e) {
14+
e.printStackTrace();
15+
}
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.exception.currentstacktrace;
2+
3+
public class StackTraceUsingThreadDemo {
4+
5+
public static void main(String[] args) {
6+
methodA();
7+
}
8+
9+
public static StackTraceElement[] methodA() {
10+
return methodB();
11+
}
12+
13+
public static StackTraceElement[] methodB() {
14+
return Thread.currentThread().getStackTrace();
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.exception.currentstacktrace;
2+
3+
public class StackTraceUsingThrowableDemo {
4+
5+
public static void main(String[] args) {
6+
methodA();
7+
}
8+
9+
public static StackTraceElement[] methodA() {
10+
try {
11+
methodB();
12+
} catch (Throwable t) {
13+
return t.getStackTrace();
14+
}
15+
return null;
16+
}
17+
18+
public static void methodB() throws Throwable {
19+
throw new Throwable("A test exception");
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.exception.currentstacktrace;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import com.baeldung.exception.currentstacktrace.StackTraceUsingThreadDemo;
8+
import com.baeldung.exception.currentstacktrace.StackTraceUsingThrowableDemo;
9+
10+
public class CurrentStacktraceDemoUnitTest {
11+
12+
@Test
13+
public void whenElementIsFecthedUsingThread_thenCorrectMethodAndClassIsReturned() {
14+
StackTraceElement[] stackTrace = new StackTraceUsingThreadDemo().methodA();
15+
16+
StackTraceElement elementZero = stackTrace[0];
17+
assertEquals("java.lang.Thread", elementZero.getClassName());
18+
assertEquals("getStackTrace", elementZero.getMethodName());
19+
20+
StackTraceElement elementOne = stackTrace[1];
21+
assertEquals("com.baeldung.exception.currentstacktrace.StackTraceUsingThreadDemo", elementOne.getClassName());
22+
assertEquals("methodB", elementOne.getMethodName());
23+
24+
StackTraceElement elementTwo = stackTrace[2];
25+
assertEquals("com.baeldung.exception.currentstacktrace.StackTraceUsingThreadDemo", elementTwo.getClassName());
26+
assertEquals("methodA", elementTwo.getMethodName());
27+
28+
StackTraceElement elementThree = stackTrace[3];
29+
assertEquals("com.baeldung.exception.currentstacktrace.CurrentStacktraceDemoUnitTest", elementThree.getClassName());
30+
assertEquals("whenElementIsFecthedUsingThread_thenCorrectMethodAndClassIsReturned", elementThree.getMethodName());
31+
}
32+
33+
@Test
34+
public void whenElementIsFecthedUsingThrowable_thenCorrectMethodAndClassIsReturned() {
35+
StackTraceElement[] stackTrace = new StackTraceUsingThrowableDemo().methodA();
36+
37+
StackTraceElement elementZero = stackTrace[0];
38+
assertEquals("com.baeldung.exception.currentstacktrace.StackTraceUsingThrowableDemo", elementZero.getClassName());
39+
assertEquals("methodB", elementZero.getMethodName());
40+
41+
StackTraceElement elementOne = stackTrace[1];
42+
assertEquals("com.baeldung.exception.currentstacktrace.StackTraceUsingThrowableDemo", elementOne.getClassName());
43+
assertEquals("methodA", elementOne.getMethodName());
44+
45+
StackTraceElement elementThree = stackTrace[2];
46+
assertEquals("com.baeldung.exception.currentstacktrace.CurrentStacktraceDemoUnitTest", elementThree.getClassName());
47+
assertEquals("whenElementIsFecthedUsingThrowable_thenCorrectMethodAndClassIsReturned", elementThree.getMethodName());
48+
}
49+
}

0 commit comments

Comments
 (0)