Printing a Stack Trace to the Server Console
In this section, you will study how the Stack Trace is printed on the Console.
When you generate an exception in a program, it will be better to print out the exception stack trace to catch the error instead of debugging jsp pages. By the stack trace, you will know which method threw an exception and how that method is called. Instead of using getMessage() method of the exception to print errors during debugging process, you can use printStackTrace() method which prints a stack trace from the exception and provides more information about the error process.
Here is an example which illustrates you more clearly.
Here is the code (stackTrace.jsp):
<HTML> <HEAD> <TITLE>Printing a Stack Trace to the ServerConsole</TITLE> </HEAD> <BODY> <H1>Print a Stack Trace to the Server Console</H1> <% try{ int value = 5; value = value / 0; } catch (Exception e){ e.printStackTrace(); } %> </BODY> </HTML> |
In the above example, we have generated an error by dividing the value by 0. Instead of using e.getMessage(), we have use e.printStackTrace() so that we can get more information about the error process.
Output will be displayed as