214k views
4 votes
Which method of the "Exception" class prints a list of methods that were called before the exception was thrown?

A. printErrors()
B. getMessage()
C. printStackTrace()
D. traceStack()
E. toString()

User Sumanta
by
8.1k points

1 Answer

0 votes

Final answer:

The method of the "Exception" class that prints a list of methods called before the exception was thrown is printStackTrace().

Step-by-step explanation:

The correct method of the "Exception" class that prints a list of methods that were called before the exception was thrown is printStackTrace().

For example, consider the following code:

public class Example {
public static void main(String[] args) {
try {
method1();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void method1() {
method2();
}

public static void method2() {
throw new Exception();
}
}

If an exception is thrown in method2(), calling e.printStackTrace() in the catch block will print the stack trace which includes the list of methods called before the exception was thrown.

User Eugene Marcotte
by
7.8k points

Related questions