aspose file tools*
The moose likes Beginning Java and the fly likes Exercise 5-4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exercise 5-4" Watch "Exercise 5-4" New topic
Author

Exercise 5-4

Barbara Flink
Greenhorn

Joined: Aug 02, 2010
Posts: 21
I'm studying the K&B book for the SCJP and I'm working on Exercise 5-4 on page 377-378

Here is my solution:



I have two questions

1) Evidently I misunderstand how "finally" works. Why is "That's all for now" printing before the stack trace?
2) I am used to editing and running my Java programs in NetBeans.
How do I pass an argument when I run a file in NetBeans?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
You should be able to pass JVM arguments via NetBeans; if you get a "run" dialogue search through that.

I suggest you get a pencil and paper, print out your code with double-spacing, and draw arrows from line to line, so you can see the flow of execution. You may have misunderstood; I tried exactly the same program and got the stack trace before "That's all". The reason may be that the stack trace prints to the standard error stream, and the other output to the standard output stream, which re like different threads, and do not necessarily print in a particular order. Also using an IDE may alter the order of output.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Barbara Flink wrote:1) Evidently I misunderstand how "finally" works. Why is "That's all for now" printing before the stack trace?

It doesn't. At least, not from my command line. The thing is, "That's all for now" goes to System.out while the stack trace goes to System.err. The console usually interweaves these two quite well, but IDE's like Eclipse and NetBeans guarantee no specific interweaving at all. In your case, apparently, NetBeans finishes the System.out part before the System.err part.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Yesterday, I wrote:. . . You may have misunderstood . . .
And looking at that now, it doesn't make sense. I think I ought to have said
You may not have misunderstood
Sorry.
Barbara Flink
Greenhorn

Joined: Aug 02, 2010
Posts: 21
Thanks guys! So it isn't me ... it's NetBeans!!!
I appreciate your help.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exercise 5-4