Java Reference
In-Depth Information
System.out.println("Line Number: " + lineNumber);
System.out.println("---------------------------");
}
}
}
Frame count: 4
Frame Index: 0
File Name: StackFrameTest.java
Class Name: com.jdojo.exception.StackFrameTest
Method Name: m3
Line Number: 21
---------------------------
Frame Index: 1
File Name: StackFrameTest.java
Class Name: com.jdojo.exception.StackFrameTest
Method Name: m2
Line Number: 14
---------------------------
Frame Index: 2
File Name: StackFrameTest.java
Class Name: com.jdojo.exception.StackFrameTest
Method Name: m1
Line Number: 10
---------------------------
Frame Index: 3
File Name: StackFrameTest.java
Class Name: com.jdojo.exception.StackFrameTest
Method Name: main
Line Number: 6
---------------------------
Now that you have access to the stack frames of a thread, you may want to know what you can do with
this information. The information about a thread's stack lets you know the location in the program where the
code is executing. Typically, you log this information for debugging purposes. If you compare the output of the
printStackTrace() method with the output of Listing 9-13, you would observe that they are similar, except that they
print the same information in different formats.
The try-with-resources Block
Java 7 added a new construct called try-with-resources . Before Java 7, when you worked with a resource, such
as a file, a SQL statement, etc., you had to use a finally block and write a few lines of boilerplate code to close the
resource. Prior to Java 7, the typical code, to work with a resource, would look as follows:
AnyResource aRes;
try {
aRes = create the resource...;
// Work with the resource here
}
 
Search WWH ::




Custom Search